Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何实现相同的布局宽度和布局高度_Android_Xml_Android Layout_Android Linearlayout - Fatal编程技术网

Android 如何实现相同的布局宽度和布局高度

Android 如何实现相同的布局宽度和布局高度,android,xml,android-layout,android-linearlayout,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

我试图实现的是将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>

但它似乎只是将按钮的比例增加了一倍,仍然保持相同的比例。

您可以创建
按钮的自定义子类,并覆盖
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
,请将
按钮声明更改为以下内容:

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

您可以创建
按钮的自定义子类,并覆盖测量上的
,例如:

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
,请将
按钮声明更改为以下内容:

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


我该如何声明我的按钮是方形按钮的子类?@Bartu我更新了我的答案,加入了解释。这个答案可能也有帮助:这解决了我的问题。但是,仅仅为了使一个按钮成为正方形并支持所有维度而将其子类化,感觉很可怕:)谢谢您的更新。我相信它也会帮助其他人。我该如何声明我的按钮是方形按钮的子类呢?@Bartu我更新了我的答案,加入了一个解释。这个答案可能也有帮助:这解决了我的问题。但是,仅仅为了使一个按钮成为正方形并支持所有维度而将其子类化,感觉很可怕:)谢谢您的更新。我相信它也会帮助其他人。