如何实现 layout_width 和 layout_height 相等
How to achieve equal layout_width and layout_height
我想要实现的是将 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");
}
然而它似乎只是将按钮的比例扩大了一倍,比例仍然相同。
您可以创建 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"
/>
我想要实现的是将 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");
}
然而它似乎只是将按钮的比例扩大了一倍,比例仍然相同。
您可以创建 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"
/>