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