Подтвердить что ты не робот

Высота макета Android в процентах

Я знаю, что невозможно установить проценты и вы можете установить вес определенных изображений для масштабирования их высот. То, что я пытаюсь сделать, это указать высоту макета относительно макета, в котором он находится. В основном у меня есть что-то вроде этого

<LinearLayout android:layout_height="fill_parent">
  <LinearLayout>

  </LinearLayout>
</LinearLayout>

Конечно, это очень упрощенная версия, просто чтобы вы могли понять мою тарабарщину. В основном я хочу, чтобы внутренний linearlayout составлял около 50% от основной линейной очереди.

Каков наилучший способ сделать это?

4b9b3361

Ответ 1

Вы можете добавить еще один пустой макет ниже этого и установить для них обоих одинаковый вес макета. Они должны получать 50% пространства каждый.

Ответ 2

Существует атрибут android: weightSum.

Вы можете установить android: weightSum = "2" в родительском linear_layout и android: weight = "1" во внутреннем linear_layout.

Не забудьте установить inner linear_layout на fill_parent, так что атрибут веса может работать как ожидалось.

Кстати, я не думаю, что его необходимо добавить второй взгляд, хотя я не пробовал.:)

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:weightSum="2">

    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

</LinearLayout>

Ответ 3

Как вы сказали, я бы рекомендовал вес. Проценты были бы невероятно полезны (не знаю, почему они не поддерживаются), но один из способов сделать это:

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    >
    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        >
    </LinearLayout>
    <View
        android:layout_height="0dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
    />
</LinearLayout>

Вывод: у вас есть пустой вид, который займет оставшееся пространство. Не идеально, но он делает то, что вы ищете.

Ответ 4

android: layout_weight = ". YOURVALUE" лучший способ реализовать в процентах

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/logTextBox"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".20"
        android:maxLines="500"
        android:scrollbars="vertical"
        android:singleLine="false"
        android:text="@string/logText" >
    </TextView>

</LinearLayout>

Ответ 5

С введением ContraintLayout можно реализовать с помощью Руководства:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eugene.test1.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#AAA"
        android:text="TextView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>

Результат

Вы можете прочитать больше в этой статье Построение интерфейсов с помощью ConstraintLayout.