How to achieve equal layout_width and layout_height

一个人想着一个人 提交于 2020-01-25 20:58:06

问题


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)

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>

I tried this code

        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.


回答1:


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

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


来源:https://stackoverflow.com/questions/28845938/how-to-achieve-equal-layout-width-and-layout-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!