Java 在约束布局上设置最大高度
我在约束布局中有一个listview。当它有更多列表时,需要限制父布局高度。如何以编程方式设置Java 在约束布局上设置最大高度,java,android,android-layout,constraints,android-constraintlayout,Java,Android,Android Layout,Constraints,Android Constraintlayout,我在约束布局中有一个listview。当它有更多列表时,需要限制父布局高度。如何以编程方式设置底部布局的最大布局高度?最大显示设备高度的80% 这是布局图: <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/bottom_layout" android:layout_width="match_parent" andro
底部布局的最大布局高度
?最大显示设备高度的80%
这是布局图:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/primary_button"
android:minWidth="180dp"
android:text="@string/error_try_again"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ListView
android:id="@+id/instruction_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/try_again"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/instruction_subtitle" />
<TextView
android:id="@+id/instruction_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/instruction_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instruction_title"
app:layout_constraintBottom_toTopOf="@+id/instruction_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以通过将
ConstraintLayout
包装到另一个高度与屏幕高度匹配的布局中,在布局上执行此操作
然后将以下约束添加到内部ConstraintLayout
以获得总屏幕高度的80%的最大高度。这需要具有与约束匹配的高度
android:layout_height="0dp"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"
因此,外部布局将是:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"
app:layout_constraintStart_toStartOf="parent">
<!-- other views -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Hi,在布局上这样做可以吗?如果您是指约束布局,那么可以。我们应该如何包含此布局以及约束值是什么?@Balasubramanian您可以用它替换共享布局。。我刚刚添加了一个内部的ConstraintLayout
,这样我们就可以使用app:layout\u constraintHeight\u percent
控制它的高度,而不是屏幕高度。。。。您可以将
替换为所有相同的视图,如说明列表
列表、尝试按钮
…我的意思是,将其创建为可重用布局。当我试图将其包含在另一个XML中时遇到问题。listview约束应该是什么?如果达到最大值,则与上述列表视图内容重叠。上视图和下视图之间不匹配。说明字幕缺少垂直约束。。。ListView与之绑定
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"
app:layout_constraintStart_toStartOf="parent">
<!-- other views -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>