我在 Android 中有一个带有内部 ConstraintLayout 和内部线性布局的 ConstraintLayout。在这里你可以看到我的代码:
<?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:id="@+id/outerConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_8sdp"
android:layout_width="@dimen/_235sdp"
android:layout_height="0dp"
android:orientation="vertical"
android:background="@drawable/rim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.93"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.75">
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/innerConstraintLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
app:layout_constraintEnd_toStartOf="@+id/linearLayout"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_30sdp"
android:background="@drawable/rim"
android:padding="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/linearLayout"
>
<TextView
android:id="@+id/textView_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner Constrained Layout"
android:textColor="#000000"
android:textSize="@dimen/_10ssp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_okay"
android:layout_width="match_parent"
android:layout_height="@dimen/_25sdp"
android:background="@color/colorGreen"
android:text="Okay"
android:textAllCaps="false"
android:textColor="#121212"
android:textSize="@dimen/_8ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.553"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
问题是两种布局在不同屏幕上显示的比例不同(见截图):
这看起来不太好,因为根据所使用的设备,布局在比例(宽度和高度)方面看起来完全不同。现在我的问题是是否有一种方法可以将布局的宽度和高度设置为屏幕尺寸的百分比,从而使布局的宽度和高度的比例与屏幕尺寸无关?或者你会如何设计布局,使其宽度和高度之间的比例或多或少独立于屏幕尺寸?
感谢您的每一条评论,如果您分享您在该问题上的经验,我会很高兴吗?