我试图设置一个按钮到60%的高度和60%的物理设备的宽度。如何在Android中进行百分比高度和宽度?
由于屏幕的大小取决于用户,所以使用骤降是不可能的。
我们如何在Android中做百分比高度和宽度?例如:
<Button android:layout_width="60%" android:layout_height="60%"/>
我试图设置一个按钮到60%的高度和60%的物理设备的宽度。如何在Android中进行百分比高度和宽度?
由于屏幕的大小取决于用户,所以使用骤降是不可能的。
我们如何在Android中做百分比高度和宽度?例如:
<Button android:layout_width="60%" android:layout_height="60%"/>
您可以使用layout_weight
属性。如果您将父级布局设置为10重量,那么您可以在Button
中设置6的权重。
有上堆栈溢出的话题颇有几分:
Linear Layout and weight in Android
...或Android官方博客:
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
现在,可以用Guidelines定位百分比值
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
app:layout_constraintEnd_toStartOf="@+id/verticalGuideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/verticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6" />
<android.support.constraint.Guideline
android:id="@+id/horizontalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
</android.support.constraint.ConstraintLayout>