如何实现平等的layout_width和layout_height [英] How to achieve equal layout_width and layout_height

查看:55
本文介绍了如何实现平等的layout_width和layout_height的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的目标是将4个按钮彼此相邻放置,并留有一定的边距,使它们为正方形(不提供固定大小)

What I am trying to achieve is to put 4 buttons next to each other, with some margins, and make them square (without supplying fixed sizes)

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

I basically have a wrapper LinearLayout that holds 4 buttons, which I want them to be square shaped.

<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");
        }

然而,似乎按钮的比例却翻了一番,仍然是相同的比例.

Yet it just seemed to double up the scale of buttons, still with same proportions.

推荐答案

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

You can create a custom subclass of Button and override onMeasure, for example:

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声明更改为以下内容:

To use SquareButton in your layout file, change your Button declarations to the following:

<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"
    />

这篇关于如何实现平等的layout_width和layout_height的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆