我希望创建一个宽度为 match_parent 的 View ,但高度为宽度的 50%。
我该怎么做?
是通过 Activity 内的java代码来做到这一点的唯一方法吗?我必须读取屏幕宽度并以这种方式设置高度。
最佳答案
您可以使用 ConstraintLayout 实现所需的任何宽高比。
要设置 ConstraintLayout,请按照以下步骤操作:Build a Responsive UI with ConstraintLayout 。 然后你可以做这样的事情:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="h,2:1"
app:layout_constraintHorizontal_bias="0.0" />
</android.support.constraint.ConstraintLayout>
关于Android 使 View 高度为屏幕宽度的 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44438996/