Равномерно расположенные кнопки фиксированного размера Android - PullRequest
0 голосов
/ 28 мая 2011

У меня есть этот документ XML.Я хочу, чтобы эти кнопки размером 93 на 93 пикселя располагались на одинаковом расстоянии.В данный момент установка веса заставляет их растягиваться и обнимать друг друга, чтобы не было пробелов.Но если я установлю вес на 0, они будут делать то же самое, обнимая друг друга, но заполняют примерно половину экрана.

Кстати, это для альбомной ориентации.

    <LinearLayout android:id="@+id/LinearLayout" android:layout_height="wrap_content"
  android:layout_width="fill_parent">
   <Button 
    android:background="@drawable/a" 
    android:layout_height="93px" 
    android:id="@+id/a"
    android:layout_width="93px" 
    android:layout_weight="1" 
    android:layout_marginLeft="20dip" 
    android:layout_marginTop="30dip"/>
  <Button 
    android:background="@drawable/b" 
    android:layout_height="93px" 
    android:id="@+id/b"
    android:layout_width="93px" 
    android:layout_weight="1"  
    android:layout_marginTop="30dip"/>

  <Button 
    android:background="@drawable/c" 
    android:layout_height="93px" 
    android:id="@+id/c" 
    android:layout_weight="1" 
    android:layout_width="93px"  
    android:layout_marginTop="30dip"/>
  <Button 
    android:background="@drawable/d" 
    android:layout_height="93px" 
    android:id="@+id/d"
    android:layout_width="93px" 
    android:layout_weight="1" 
    android:layout_marginRight="20dip" 
    android:layout_marginTop="30dip" />
    </LinearLayout>

a

Ответы [ 5 ]

4 голосов
/ 28 мая 2011

Решение простое.

Вы должны держать каждую кнопку в отдельном линейном макете и поддерживать вес = 1 для каждого линейного макета. Теперь кнопки будут располагаться одинаково независимо от разрешения или ориентации.

EX: -

      <LinearLayout android:id="@+id/LinearLayout" android:layout_height="wrap_content"
      android:layout_width="fill_parent">
      <LinearLayout android:id=@+id/Button1Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">  
       <Button 
        android:background="@drawable/a" 
        android:layout_height="93px" 
        android:id="@+id/takequiz"
        android:layout_width="93px" 
        android:layout_weight="1" 
        android:layout_marginLeft="20dip" 
        android:layout_marginTop="30dip"/>
</LinearLayout>
<LinearLayout android:id=@+id/Button2Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">
      <Button 
        android:background="@drawable/b" 
        android:layout_height="93px" 
        android:id="@+id/random"
        android:layout_width="93px" 
        android:layout_weight="1"  
        android:layout_marginTop="30dip"/>
</LinearLayout>
<LinearLayout android:id=@+id/Button3Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">

      <Button 
        android:background="@drawable/c" 
        android:layout_height="93px" 
        android:id="@+id/search" 
        android:layout_weight="1" 
        android:layout_width="93px"  
        android:layout_marginTop="30dip"/>
</LinearLayout>
<LinearLayout android:id=@+id/Button4Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">

      <Button 
        android:background="@drawable/d" 
        android:layout_height="93px" 
        android:id="@+id/browse"
        android:layout_width="93px" 
        android:layout_weight="1" 
        android:layout_marginRight="20dip" 
        android:layout_marginTop="30dip" />
        </LinearLayout>
</LinearLayout>
2 голосов
/ 23 августа 2011

Попробуйте это:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#000000"
        android:id="@+id/linearLayout01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
    >
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn1"
            android:layout_height="93px"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
        ></Button>
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn2"
            android:layout_height="93px"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
        ></Button>
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn3"
            android:layout_height="93px"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
        ></Button>
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn4"
            android:layout_height="93px"
            android:layout_weight="1"
        ></Button>
    </LinearLayout>
1 голос
/ 28 мая 2011

LinearLayout не имеет внутреннего понятия "равный интервал". Вы можете рассмотреть возможность использования макета, разработанного для «панели мониторинга», например https://gist.github.com/882650.

0 голосов
/ 28 мая 2011

Просто установите поле влево или поле вправо для любой из кнопок

0 голосов
/ 28 мая 2011

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/LinearLayout" 
   android:layout_height ="wrap_content"
   android:layout_width  ="fill_parent" 
   android:orientation   ="horizontal"
   android:layout_gravity="center_horizontal">

   <Button  
    android:id="@+id/takequiz"  
    android:text="take quiz"
    android:layout_height="93px"
    android:layout_width ="93px" 
    android:layout_marginTop ="30dip"/>

  <Button 
    android:id="@+id/random" 
    android:text="random"
    android:layout_height="93px" 
    android:layout_width ="93px" 
    android:layout_marginTop="30dip"/>

  <Button 
    android:id="@+id/search" 
    android:text="search"
    android:layout_height="93px"
    android:layout_width="93px" 
    android:layout_marginTop="30dip"/>

  <Button
    android:id="@+id/browse"
    android:text="browse"
    android:layout_height="93px" 
    android:layout_width="93px" 
    android:layout_marginTop="30dip" />

 </LinearLayout>

Вы рассматривали 2 ряда? Я сделал подобное для приложения типа Sound Board, использующего TableLayout. Хотя в эти дни я склонен использовать RelativeLayout больше. Вот код таблицы.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/LinearLayout" 
   android:layout_height ="fill_parent"
   android:layout_width  ="fill_parent" >
   <TableRow
      android:layout_height ="wrap_content"
      android:layout_width  ="fill_parent" 
      android:gravity       ="center">    
       <Button  
        android:id  ="@+id/takequiz"  
        android:text="take quiz"
        android:layout_height="93px"
        android:layout_width ="93px" />    
      <Button 
        android:id  ="@+id/random" 
        android:text="random"
        android:layout_height="93px" 
        android:layout_width ="93px" />
    </TableRow> 
    <TableRow
      android:layout_height ="wrap_content"
      android:layout_width  ="fill_parent" 
      android:gravity       ="center">    
        <Button 
          android:id  ="@+id/search" 
          android:text="search"
          android:layout_height="93px"
          android:layout_width ="93px" />       
        <Button
          android:id  ="@+id/browse"
          android:text="browse"
          android:layout_height="93px" 
          android:layout_width ="93px"  />
    </TableRow>
 </TableLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...