2013-09-10 6 views
0

Я новичок в разработке Android, и мне было просто интересно, как я могу создать три кнопки с равномерным распределением в нижней части экрана. Мне также было интересно, какой стиль я мог бы использовать, чтобы эти кнопки выглядели аккуратно и аккуратно.Как создать три кнопки с равномерным интервалом

Код, который я написал до сих пор.

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/Fragment1" 
       android:id="@+id/worldTextView" /> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <Button 
      android:id="@+id/Button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Button1" 
      /> 

      <Button 
       android:id="@+id/Button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/Button2" 

       /> 

      <Button 
      android:id="@+id/Button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Button3" 
      /> 
    </LinearLayout> 

ответ

0

Использование android:layout_width="0dp" и android:layout_weight="1" и сделать ширину линейной компоновки android:layout_width="match_parent"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" // wrap_content to match_parent 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Fragment1" 
       android:id="@+id/worldTextView" /> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" // wrap_content t0 match_parent 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <Button 
      android:id="@+id/Button1" 
      android:layout_width="0dp" 
      android:layout_weight="1" // specify the layout weight 
      android:layout_height="wrap_content" 
      android:text="@string/Button1" 
      /> 

      <Button 
       android:id="@+id/Button2" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="@string/Button2" 

       /> 

      <Button 
      android:id="@+id/Button3" 
       android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/Button3" 
      /> 
    </LinearLayout> 
    </LinearLayout> 

ФОТОГРАФИЯ

enter image description here

Edit:

Вы можете также использовать относительный макет.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Fragment1" /> 

    <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:orientation="horizontal"> 
      <Button 
       android:id="@+id/Button1" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:text="Button1" 
       /> 

       <Button 
        android:id="@+id/Button2" 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:text="Button2" 

        /> 

       <Button 
       android:id="@+id/Button3" 
        android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="Button3" 
       /> 
     </LinearLayout> 


     </RelativeLayout> 
1

Держите три кнопки в другой компоновке (линейная компоновка с горизонтальной ориентацией) внизу и придайте вес каждой кнопке, а layout_width - как ноль.

0

вы должны использовать android:layout_weight для всех трех кнопок.

<Button 
    android:id="@+id/Button1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Button1" 
    android:layout_weight="1"/> 
1

Использование Relative Layout .. Слишком много линейных Макеты вызовет перегрузку нагрузки при визуализации файла макета XML.

0

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

<LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <Button 
      android:id="@+id/Button1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/Button1" 
      android:layout_weight="1" 
      /> 

      <Button 
       android:id="@+id/Button2" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="@string/Button2" 
       android:layout_weight="1" 
       /> 

      <Button 
      android:id="@+id/Button3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/Button3" 
      android:layout_weight="1" 
      /> 
    </LinearLayout> 
0

Дайте LinearLayout в weightSum атрибут и его ребенок назначить layout_weight атрибут, как в приведенном ниже коде.

Затем укажите высота и ширина родительского элемента и дочернего элемента match_parent. Это позволит настроить ваш макет высоты и ширины в соответствии с разрешением экрана и высотой и шириной макета.

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:weightSum = "3"> 
    <Button 
     android:id="@+id/Button1" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/Button1" 
     /> 

     <Button 
      android:layout_weight="1" 
      android:id="@+id/Button2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@string/Button2" 

      /> 

     <Button 
     android:layout_weight="1" 
     android:id="@+id/Button3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/Button3" 
     /> 

+0

Установить высоту как 'match_parent'? Зачем вам нужны три кнопки, охватывающие весь экран? – Vikram

1

Для равномерного пространства ваших кнопок, вы можете установить для каждых кнопок android:layout_width на 0 и его android:layout_weight к 1, чтобы сделать ширину каждой кнопки то же самым.

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <Button 
     android:id="@+id/Button1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/Button1" 
     android:layout_weight="1"/> 
    <Button 
     android:id="@+id/Button2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/Button2" 
     android:layout_weight="1"/> 
    <Button 
     android:id="@+id/Button3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/Button3" 
     android:layout_weight="1"/> 
</LinearLayout> 

Ваш линейный макет также нужен layout_width, который отличен от нуля и не зависит от его детей (например, wrap_content). Я использовал fill_parent в этом примере, но вы также можете использовать match_parent или твердое кодированное значение, такое как 55dp.

This tutorial является достойным углубленным руководством по использованию android:layout_weight.

Смежные вопросы