2016-08-02 96 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