问题
我有一个带有以下XML的按钮:
android:id="@+id/firstNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/firstNumber"
app:layout_constraintEnd_toStartOf="@+id/secondNumber"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
当前,按钮的layout_width设置为match_constraint(这是我认为其显示为“ 0dp”的原因).
我想将按钮制作成正方形,其宽度等于高度.这是我到目前为止找到的解决方案:
解决方案1:
我可以做类似于this answer的操作,在该操作中,我将XML layout_width和layout_height值更改为特定值@ dimen / box_size:
android:layout_width="@dimen/box_size"
android:layout_height="@dimen/box_size"
但是,这并不是理想的方法,因为它基本上是对按钮的宽度进行硬编码,对于不同的屏幕尺寸(与使用match_constraint相对),调整得不好.
解决方案2:
我可以编写一些Java代码来做到这一点(source):
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());
}
}
这会起作用,但我宁愿在XML中执行此操作.有办法吗?
解决方法:
尝试以下方法并检查
You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimensionRatio to a given ratio.
android:id="@+id/firstNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="First Number"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@+id/secondNumber"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Please refer below code, for put four square button in horizontal line
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/firstNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="FN"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/secondNumber"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/secondNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="SN"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toRightOf="@+id/firstNumber"
app:layout_constraintRight_toLeftOf="@+id/thirdnumber"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/thirdnumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TN"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toRightOf="@+id/secondNumber"
app:layout_constraintRight_toLeftOf="@+id/fourtnumber"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/fourtnumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:text="FON"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toRightOf="@+id/thirdnumber"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
有关更多信息,请参阅此Link中的比率
水平的四个动态正方形imageview
标签:android-layout,xml,android
来源: https://codeday.me/bug/20191108/2009558.html