2

使用 3 行和 4 列,我尝试将所有按钮水平均匀地隔开,但失败了。垂直很好。

下面是 3 种不同的间隔按钮的尝试,我的 3 行中的每行 1 次。第一行拉伸图形(这是不需要的),第二行不拉伸但没有间距(图形之间需要间距),而第三行拉伸时没有任何间距。请帮助我将它们均匀分布。同一站点上的其他人的解决方案没有奏效。我应该尝试改用桌子吗?

视觉总结= 不拉伸图形:我想要 | xxxx | 而不是| xxxx |

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

<LinearLayout
    android:id="@+id/row1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight=".1" >
    </View>

    <Button
        android:id="@+id/a1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/blanktilesml"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/clear64" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight=".1" >
    </View>

    <Button
        android:id="@+id/a2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/blanktilesml"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/clear64" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight=".1" >
    </View>

    <Button
        android:id="@+id/a3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/blanktilesml"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/clear64" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight=".1" >
    </View>

    <Button
        android:id="@+id/a4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/blanktilesml"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/clear64" />

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

<LinearLayout
    android:id="@+id/row2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/b1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blanktilesml"
            android:gravity="center"
            android:src="@drawable/clear64" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blanktilesml"
            android:gravity="center"
            android:src="@drawable/clear64" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/b3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blanktilesml"
            android:gravity="center"
            android:src="@drawable/clear64" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/b4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blanktilesml"
            android:gravity="center"
            android:src="@drawable/clear64" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/row3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:id="@+id/c1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:background="@drawable/blanktilesml"
        android:paddingLeft="10dp"
        android:src="@drawable/clear64" />

    <Button
        android:id="@+id/c2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:background="@drawable/blanktilesml"
        android:paddingLeft="10dp"
        android:src="@drawable/clear64" />

    <Button
        android:id="@+id/c3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:background="@drawable/blanktilesml"
        android:paddingLeft="10dp"
        android:src="@drawable/clear64" />

    <Button
        android:id="@+id/c4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:background="@drawable/blanktilesml"
        android:paddingLeft="10dp"
        android:src="@drawable/clear64" />
</LinearLayout>

</LinearLayout>
4

2 回答 2

7

尝试这个:

<LinearLayout
    android:id="@+id/row1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/a1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:drawable/arrow_up_float"
            android:gravity="center_horizontal|center_vertical"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/a2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:drawable/arrow_up_float"
            android:gravity="center_horizontal|center_vertical"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/a3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:drawable/arrow_up_float"
            android:gravity="center_horizontal|center_vertical"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/a4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:drawable/arrow_up_float"
            android:gravity="center_horizontal|center_vertical"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

试试这个作为你的第一行。同样可以对所有行进行,

因此,除非您不希望图像被拉伸,否则您需要在布局中对它们说唱。这样做您将管理间距,并且drawable看起来会很好。如果你拉伸按钮,那么drawable也将相应地拉伸。

于 2013-01-13T07:49:15.077 回答
0

这是我正在处理的一个计算器项目中所做的。要求行中的按钮大小都相同。您需要为按钮添加填充以满足您的需求。:

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="5dp"
        android:id="@+id/NumberPad">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/NumberRow123"
            android:layout_weight="1">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button1"
                android:layout_weight="1"
                android:background="@drawable/unpressed7"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button2"
                android:layout_weight="1"
                android:background="@drawable/unpressed8"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/button3"
                android:layout_weight="1"
                android:background="@drawable/unpressed9"/>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/NumerRow456"
            android:layout_weight="1">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button4"
                android:layout_weight="1"
                android:background="@drawable/unpressed4"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button5"
                android:layout_weight="1"
                android:background="@drawable/unpressed5"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button6"
                android:layout_weight="1"
                android:background="@drawable/unpressed6"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:id="@+id/NumberRow789">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button7"
                android:layout_weight="1"
                android:background="@drawable/unpressed1"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button8"
                android:layout_weight="1"
                android:background="@drawable/unpressed2"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/button9"
                android:layout_weight="1"
                android:background="@drawable/unpressed3"/>

        </LinearLayout>
于 2013-01-13T07:24:50.963 回答