所有按钮的宽度相等

时间:2016-08-02 08:51:56

标签: android android-xml android-button

我想实现以下布局。

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>

2 个答案:

答案 0 :(得分:0)

从LinearLayout中取出零按钮,我假设有另一个布局,所有这些都被定义,如相对布局?并尝试这种效果。

GotFocus

答案 1 :(得分: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

相关问题