2016-08-02 5 views
-1

Я хочу достичь следующей компоновки.Равная ширина всех кнопок

enter image description here

Как сделать кнопку 0 равно остальных кнопок. Обратите внимание, что я использовал layout_weight = "1", так что все остальные кнопки имеют равную длину при совпадении с родителем. Поскольку я создал кнопку 0 на другой компоновке, поэтому я не могу сделать ее равной длины с другими кнопками.

Вот мой код до сих пор

<LinearLayout 

    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:id="@+id/seven" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/eight" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/nine" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<Button 
    android:id="@+id/zero" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 
+0

создать две «фиктивные» кнопки, невидимые, по бокам кнопки «0». Но позвольте мне предложить вам использовать GridView, а не пучок LinearLayouts. –

ответ

0

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

<LinearLayout 

    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/linearLayout"> 
    <Button 
     android:id="@+id/seven" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/eight" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/nine" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

<Button 
    android:id="@+id/zero" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/linearLayout" 
    android:layout_marginTop="98dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

0

Возьмите последнюю кнопку в другом LinearLayout и далее добавить LinearLayout в нем с весом, попробуйте этот код ....

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/seven" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/eight" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/nine" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"></LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <Button 
      android:id="@+id/zero" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1"></LinearLayout> 
</LinearLayout> 

here is the output

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