将布局限制为屏幕高度的50%?

时间:2020-10-15 20:37:52

标签: android xml

我在Android Studio中有一个ConstraintLayout,我希望它的高度是运行于任何设备上的屏幕尺寸的60%,我想知道如何在xml中做到这一点?

目前,我有:

<androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/recycler_view_container"
            android:layout_width="match_parent"
            android:layout_height="372dp"   // BAD!! HARDCODED
            android:background="@color/white">

</androidx.constraintlayout.widget.ConstraintLayout>

我该怎么做?如您所见,layout_height现在已被硬编码。

3 个答案:

答案 0 :(得分:2)

您不能直接执行此操作,但是可以将ConstraintLayout内的指导原则固定为高度的百分比。然后,子视图可以使用该约束来设置其高度。

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.6"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv"
        android:layout_height="0dp"
        android:layout_width="0dp"
        app:layout_constraintBottom_toTopOf="@id/guide"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

我猜您的ConstraintLayout应该包含RecyclerView吗?

答案 1 :(得分:0)

您将要使用不相等的LayoutWeight。

LinearLayout支持使用android:layout_weight属性为单个子项分配权重。此属性根据视图应在屏幕上占据多少空间来为其分配“重要性”值。将子视图layout_height设置为0,然后根据需要将屏幕比例分配给每个子视图。

这就是我为您的规范设置XML的方式,代码中包含注释。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"> // Note: set orientation to vertical 

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" // Note: Set the layout_height to 0
        android:layout_weight="6" // Note: Set weight to 6
        android:background="@color/colorAccent"/>

     // Framelayout need to fill remain space
    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp" // Set the layout_height to 0
        android:layout_weight="4"/> // set the weight to 4
</LinearLayout>

约束布局的权重为6,FrameLayout的权重为4,LinearLayout的权重为10。6/10为60%,约束布局将占满屏幕的60%。

我发布此答案后,问题已经改变,您也可以平均分配,这与不平等地分配给每个子视图相同的权重值相同,并且屏幕将在子视图之间平均分配。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorAccent"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

ConstraintLayout的权重为1,FrameLayout的权重为1,总的布局权重为2。1/2为50%,ConstraintLayout将占据屏幕的50。

答案 2 :(得分:0)

这是我的答案。我认为这应该清除您的要求。

<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"
android:background="@color/colorWhite">

<androidx.constraintlayout.widget.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/colorRed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.50">

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

您可以根据需要更改身高百分比。谢谢。

相关问题