Равномерно расположенная кнопка в макете - PullRequest
11 голосов
/ 24 января 2011

Я пытаюсь сделать 4 кнопки, равномерно расположенные в портретной ориентации на Android.

Пространство должно увеличиваться и уменьшаться в зависимости от размера экрана, с равномерным расстоянием между каждой кнопкой и границами.

Я пытался использовать линейное расположение, вес и гравитацию расположения, но кажется, что я не могу центрировать кнопку по вертикали.

Это ошибка макета Android? Скорее всего, только я.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_f"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_b"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn_a"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center"
>
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
>
</Button>
</LinearLayout>
</LinearLayout>

Ответы [ 3 ]

20 голосов
/ 24 января 2011

Попробуйте что-то вроде этого:

<?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:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        >
        <Button
            android:id="@+id/btn_f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="btn_f"
            />
    </LinearLayout>
    <!-- etc -->
</LinearLayout>

Изменения по сравнению с вашей первоначальной компоновкой состоят в том, что каждая внутренняя LinearLayout имеет высоту нуля и гравитацию (не layout_gravity) центра.Это должно привести к тому, что все внутренние линейные макеты будут иметь одинаковую высоту, равномерно разделяя родительскую высоту, не растягивая сами кнопки.

3 голосов
/ 02 марта 2011

Попробуйте:

 <LinearLayout         
  android:layout_width="wrap_content"         
  android:layout_height="0dp"         
  android:orientation="horizontal"         
  android:weightSum="1"         
  android:gravity="center">
     <Button             
        android:id="@+id/btn_f"    
        android:layout_width="0dp" 
        android:layout_weight="0.5" 
        android:layout_height="wrap_content"      
        android:text="btn_f" />     
 </LinearLayout> 

Это позволит кнопке занимать только половину длины экрана.

1 голос
/ 24 января 2011

Вы, кажется, оборачиваете все кнопки в другую LinearLayout вместо того, чтобы просто держать их в родительском LinearLayout.

Удалите все эти непосредственные LinearLayout, чтобы android:layout_weight вступил в силу.


Для достижения растяжения и интервала

  • используйте android:layout_width="fill_parent" в самих кнопках
  • укажите поле для интервала
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...