Android 布局宽度等高
在我的xml中,有没有一种方法使高度取决于宽度 我有一些线性布局的按钮。按钮的宽度取决于设备,因为它们水平填充屏幕。我想要方形按钮,这是我的问题。 有人能帮我吗Android 布局宽度等高,android,android-layout,button,Android,Android Layout,Button,在我的xml中,有没有一种方法使高度取决于宽度 我有一些线性布局的按钮。按钮的宽度取决于设备,因为它们水平填充屏幕。我想要方形按钮,这是我的问题。 有人能帮我吗 <Button android:id="@+id/b_0xa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layou
<Button
android:id="@+id/b_0xa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:text="@string/b_0xa" />
您可以在Java代码中通过编程轻松地完成这项工作 动态获取按钮的宽度[使用
int x=mButton.getWidth()
],然后使用返回的值设置高度[mButton.setHeight(x)
]
p.S.:建议使用LayoutParams设置任何视图的高度和宽度。因此,不应使用setHeight(),而应使用setLayoutParams()
mButton.setLayoutParams(新的LinearLayout.LayoutParams(x,x))
您应该通过为android:layout\u width
和android:layout\u height
指定固定值来更改给定的值
<Button
android:id="@+id/b_0xa"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:text="@string/b_0xa" />
为它们指定固定值可以确保无论屏幕大小如何,按钮都保持不变,因为使用的单位是dp。请确保您指定的值相同,因为您需要一个正方形。在这种情况下,我建议为您的高度属性使用布局权重,然后它将为按钮指定一个比例权重,该按钮将在不同的屏幕大小上缩放:
<Button
android:id="@+id/b_0xa"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".4"
android:clickable="false"
android:text="@string/b_0xa" />
为了使此方法起作用,必须对布局中其他视图的高度应用权重。总权重必须等于1,因此您必须调整布局和权重以实现最佳设计。如我所述,如果您希望100%从XML执行此操作,可以使用PercentRelativeLayout并执行以下操作:
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
app:layout_aspectRatio="100%"
app:layout_widthPercent="100%" />
</android.support.percent.PercentRelativeLayout>
可以使用约束布局来实现这一点。我正在使用纵横比属性和按钮宽度的绝对值。当我的按钮按其宽度点击约束时,它将相应地设置我的高度
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@+id/b_0xa"
android:clickable="false"
android:text="@string/app_name"
android:layout_width="300dp"
android:layout_height="0dp"
android:src="@android:drawable/dark_header"
app:layout_constraintDimensionRatio="h,16:9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
确保提供此代码或手动更改高度和宽度以使视图的高度等于视图的宽度,可以使用
ConstraintLayout
app:layout_constraintDimensionRatio="1:1"
之前的1
:
表示宽度
1
后面的:
表示高度
请尝试以下代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Button"/>
</androidx.constraintlayout.widget.ConstraintLayout>
最初,您可以通过xml将默认尺寸设置为“包装内容”,当活动开始时,获取按钮的显示高度和宽度,然后根据设备大小以编程方式更改尺寸
app:layout_constraintDimensionRatio="1:1"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Button"/>
</androidx.constraintlayout.widget.ConstraintLayout>