Android LinearLayout и проблема весов
У меня проблемы с LinearLayout и весами в Android. Я хочу, чтобы горизонтальный LinearLayout содержал 2 вертикальных LinearLayout, разделенных одним представлением с 9-фоновым фоном, который будет разделителем между двумя вертикальными LinearLayouts. Вот так: (внешний блок - это внешний LinearLayout, а средняя двойная линия - мой разделитель 9 патчей.)
----------------------------
| one || three |
| two || four |
----------------------------
То, что продолжает происходить, это первые внутренние LinearLayout, отображающие с минимальной шириной для отображения его содержимого (как если бы его ширина была wrap_content), затем остальное пространство занимает представление разделителя, растянутое для заполнения остальной части внешнего LinearLayout. 2-я внутренняя LinearLayout вообще не работает.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/divider_vertical"
android:layout_weight="0" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</LinearLayout>
</LinearLayout>
Что я здесь не так делаю? Я не могу понять на всю жизнь, почему средний вид занимает все пространство, не оставляя ничего для второго внутреннего LinearLayout... Я могу заставить его работать, если я укажу определенную ширину в пикселях или dp для 9- представление патча, но я действительно хочу, чтобы оно работало без указания этой ширины. Таким образом, если я решу изменить свой 9-патч, мне не придется обновлять ширину вручную.
5 ответов
Попробуйте этот макет
Обновленный макет
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="88dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</LinearLayout>
Насколько большой ваш 9 патч? растягиваемые области патча 9 не сжимаются - ширина файла актива будет определять минимальное пространство, которое он будет пытаться занять в вашем LinearLayout.
- Удалить
weightSum="2"
от родителя LinearLayout. - Удалить
android:layout_weight="0"
из 9 патчей
Установите вес макета, как показано ниже
имейте в виду weightSum родительского элемента, по умолчанию он установлен на детей layout_weight sum
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="3" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/divider_vertical"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="3" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</LinearLayout>
</LinearLayout>
конечно, вы можете поэкспериментировать со значениями для весов
Обновитесь здесь и попробуйте
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@drawable/divider_vertical"
/>