Android 如何实现相同的布局宽度和布局高度
我试图实现的是将4个按钮相邻放置,留有一些边距,并使它们成正方形(不提供固定大小) 我基本上有一个包装器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
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我更新了我的答案,加入了一个解释。这个答案可能也有帮助:这解决了我的问题。但是,仅仅为了使一个按钮成为正方形并支持所有维度而将其子类化,感觉很可怕:)谢谢您的更新。我相信它也会帮助其他人。