如何实现相等的layout_width和layout_height

时间:2015-03-04 03:01:05

标签: android xml android-layout android-linearlayout

我想要实现的是将4个按钮彼此相邻,带有一些边距,并使它们成方形(不提供固定尺寸)

我基本上有一个包装器LinearLayout,它包含4个按钮,我希望它们是方形的。

<LinearLayout
        android:id="@+id/photos_photoWrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orienation="vertical">

        <Button
            android:id="@+id/photos_photoButton1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
                />


        <Button
            android:id="@+id/photos_photoButton2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/photos_photoButton3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            />

        <Button
            android:id="@+id/photos_photoButton4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            />
    </LinearLayout>

我试过这段代码

        Button iv = (Button)findViewById(R.id.photos_photoButton1);
        boolean bPost = iv.post(
                new Runnable()
                {
                    public void run()
                    {
                        Button iv = (Button)findViewById(R.id.photos_photoButton1);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)iv.getLayoutParams();
                        params.width = findViewById(R.id.photos_photoButton1).getWidth();
                        params.height = params.width;
                        iv.setLayoutParams(params);
                    }
                });

        if (bPost == false)
        {
            throw new RuntimeException("runnable not posted");
        }

然而,它似乎只是将按钮的比例增加了一倍,仍然具有相同的比例。

1 个答案:

答案 0 :(得分:4)

您可以创建Button的自定义子类并覆盖onMeasure,例如:

public class SquareButton extends Button {

    public SquareButton(Context context) {
        super(context);
    }

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
    }
}

要在布局文件中使用SquareButton,请将Button声明更改为以下内容:

<your.package.name.SquareButton
    android:id="@+id/photos_photoButton1"
    android:layout_width="[DESIRED WIDTH]dp"
    android:layout_height="0dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    />