Android 将布局限制为屏幕高度的50%?
我在Android studio中有一个ConstraintLayout,我希望它的高度是运行在任何设备上的屏幕大小的60%,我想知道如何在xml中做到这一点 目前我有:Android 将布局限制为屏幕高度的50%?,android,xml,Android,Xml,我在Android studio中有一个ConstraintLayout,我希望它的高度是运行在任何设备上的屏幕大小的60%,我想知道如何在xml中做到这一点 目前我有: <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/recycler_view_container" android:layout_width="match_p
<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\u height
现在是硬编码的。您不能直接这样做,但是您可以在ConstraintLayout
中设置一个指导原则,该指导原则固定在高度的百分比上。然后,子视图可以使用该约束设置其高度
我猜你的ConstraintLayout应该包含一个RecyclerView?你不能直接这样做,但是你可以在你的
ConstraintLayout中放置一个指导原则,固定在高度的百分比上。然后,子视图可以使用该约束设置其高度
我猜您的ConstraintLayout应该包含一个RecyclerView?您将要使用不相等的布局权重
LinearLayout支持使用android:layout\u weight属性为单个子级分配权重。此属性根据视图在屏幕上应占据的空间大小为视图指定“重要性”值。将子视图布局高度设置为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>
//注意:将方向设置为垂直
//框架布局需要填充剩余空间
//将重量设置为4
约束布局的权重为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。您需要使用不等的布局权重
LinearLayout支持使用android:layout\u weight属性为单个子级分配权重。此属性根据视图在屏幕上应占据的空间大小为视图指定“重要性”值。将子视图布局高度设置为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>
//注意:将方向设置为垂直
//框架布局需要填充剩余空间
//将重量设置为4
约束布局的权重为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%,约束窗口将占据屏幕的50。这是我的答案。我认为它应该明确你的要求
<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>
您可以根据需要更改高度百分比。谢谢。这是我的答案。我认为它应该明确你的要求
<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>
您可以根据需要更改高度百分比。谢谢。是的,约束布局包含回收视图,我会尝试一下,谢谢!是的,约束布局包含回收视图,我会尝试一下,谢谢!