Как я могу заставить весы Android LinearLayout работать со значениями 1 и 2?

Я пытаюсь сделать LinearLayout с тремя строками, каждая из которых имеет одинаковую высоту. Я делаю это, используя 3 LinearLayouts внутри основного, каждый с layout_weight равным 1.

Средний LinearLayout должен содержать ImageView, TextView, ImageView с весами 1,2,1. Для этого внутри среднего LinearLayout я создал еще 3.

Я хочу, чтобы TextView был вдвое больше каждого ImageView. Если вы видите код ниже, когда layout_weight равен 1 для внутренних LinearLayouts, у меня нет проблем, все три имеют одинаковый размер. Однако, когда я устанавливаю для среднего (с id=textLayout) значение layout_weight=2, он полностью исчезает (как видно в средстве просмотра графического макета Eclipse), и видны только два других.

<?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"
    android:background="@drawable/fondo"
    android:gravity="center_vertical|center_horizontal">

    <LinearLayout
        android:id="@+id/LayoutTop"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LayoutMid"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">

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

        <LinearLayout
            android:id="@+id/textLayout"
            android:orientation="horizontal"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2">
            <TextView
                android:text="Status"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            </TextView>
        </LinearLayout>

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

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LayoutBottom"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

</LinearLayout>



Ответы (1)


Вам не нужны дополнительные макеты для правильного размещения среднего ряда, и ваши веса расположены в обратном направлении. Если вы хотите, чтобы текст был в 2 раза больше внешних изображений, вы хотите, чтобы он был равен 1, а внешние столбцы были равны 1,5.

<?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">
    <LinearLayout android:id="@+id/LayoutTop"
        android:orientation="horizontal" android:layout_height="0dp"
        android:layout_width="fill_parent" android:layout_weight="1" android:background="#ffff0000"/>


    <LinearLayout android:id="@+id/LayoutMid"
        android:orientation="horizontal" android:layout_height="0dp"
        android:layout_width="fill_parent" android:layout_weight="1">

        <ImageView android:layout_height="fill_parent"
            android:layout_width="fill_parent" android:background="@drawable/icon"
            android:layout_weight="1.5" />
        <TextView android:layout_height="fill_parent" android:id="@+id/TextView01"
            android:layout_weight="1" android:text="Status" android:layout_width="fill_parent" />
        <ImageView android:layout_height="fill_parent"
            android:layout_weight="1.5" android:background="@drawable/icon"
            android:layout_width="fill_parent" />
    </LinearLayout>

    <LinearLayout android:id="@+id/LayoutBottom"
        android:orientation="horizontal" android:layout_height="0dp"
        android:layout_width="fill_parent" android:layout_weight="1" android:background="#ff00ff00"/>


</LinearLayout>

Я использовал значок (если он у вас есть), чтобы поместить изображение в средний ряд, и несколько цветов вверху и внизу, чтобы более четко показать расстояние.

person jkhouw1    schedule 30.04.2011
comment
То, что я пытаюсь сделать, это получить текст посередине, а также левую и правую кнопки с каждой стороны. Было бы лучше использовать RelativeLayout? - person Sandy; 30.04.2011
comment
вы можете сделать то же взвешивание для кнопок, что и для изображений. относительные макеты хороши, но мне часто трудно получить точное представление, это представление в 2 раза больше, чем два других решения с ними, особенно когда вы не знаете размер экрана конечного устройства. - person jkhouw1; 30.04.2011
comment
На самом деле это не ДОЛЖНО быть 2x. Он будет работать, если стрелки находятся рядом с центром TextView. Я пробовал следующий XML для RelativeLayout, и у меня возникла та же проблема... TextView не виден: (это всего лишь средняя строка) - person Sandy; 30.04.2011
comment
с относительными макетами вы не можете иметь текстовое представление в другом макете, чем изображения. используя то, что вы разместили в комментариях, вы должны иметь изображения в LayoutMid и относительно MainText или относительно layoutMid - person jkhouw1; 30.04.2011