Линейный макет (определение в процентах - layout_weight) Android - PullRequest
0 голосов
/ 03 августа 2011

1. Вопрос

Мне бы хотелось иметь полноэкранный линейный макет с 2 строками, в то время как первая строка разделена на 2 раздела, где один раздел занимает 30%, а второй 70% ширины, а вторая строка разделена на 2 раздела. одинаковый размер (каждые 50% ширины). Кроме того, первая строка занимает только 33% высоты экрана.

Я думаю, что использование android: layout_weight - правильный путь. Но это немного сложнее, так как иногда оно применяется к android: layout_width , а иногда к android: layout_height атрибуту, и применяется к одному или любому из них в зависимости от значений соответствующих атрибутов XML.

После экспериментов я обнаружил, что если установить значение атрибута на wrap_content , оно будет переопределено настройкой android: layout_weight , например. Следующий код применяет андроид : layout_weight к android: layout_width :

 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:background="#FF0000"/> 

Это правильный подход?

2. Пример

Для того, чтобы унизить макет (см. Изображение), мне нужно сделать это:

<LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#FF0000"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="2"
      android:background="#00FF00"/> 
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2">
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000FF"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000AA"/>      
  </LinearLayout>   
</LinearLayout>

enter image description here

Спасибо Sten

1 Ответ

1 голос
/ 03 августа 2011
< LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

<LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#FF0000"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="2"
      android:background="#00FF00"/> 
  </LinearLayout>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000FF"/> 
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#0000AA"/>      
  </LinearLayout>   
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...