使用 ConstraintLayout Kotlin 视图超出屏幕
View goes out of screen with ConstraintLayout Kotlin
我需要使用两个 TextView 构建 UI,这两个 TextView 被第一个父级约束,第二个被约束在第一个 TextView 之下。当 text_one 有更多文本并将 text_two 带出屏幕时,问题就来了。期望的行为是当 text_one 有更多文本时 text_two 底部与父级对齐并且不去屏幕外。类似 weight 9:1 的东西,但这只有在 text_one 有更多文本时才需要实现。屏幕 1 显示正常行为,屏幕 2 显示问题。
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FF00"
android:text="the majority have suffered alteration in some form etc..."
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@color/colorPrimary"
android:text="It is a long established etc......"
app:layout_constraintTop_toBottomOf="@+id/txt_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以利用
app:layout_constraintHeight_max
属性 约束布局。
以编程方式使用
setMaxHeight()
在您的用例中,当屏幕渲染获取屏幕高度时,
val text1MaxHeight = screenHeight*0.9
text1.setMaxHeight(text1MaxHeight)
应该可以。
但是如果你的textview1会有更多的内容,你会希望它可以滚动,所以一个新的解决方案建议你将textview1封装在scrollView中,并在NestedScrollView中使用scrollview和textview2,如下所示
<NestedScrollView>
<ScrollView>
<TextView1/>
<ScrollView/>
<TextView2/>
<NestedScrollView>
使ScrollView中的nestedScrollingEnabled标志为false,动态设置ScrollView的maxHeight
尝试下面的示例,它保持另一个 textView
高度,即使第一个在屏幕上占据更大的 space
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FF00"
android:textSize="35sp"
android:text="the majority
e suffered alteration in some form etc..."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/txt_two"
/>
<TextView
android:id="@+id/txt_two"
android:layout_width="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="25sp"
android:background="@color/colorPrimary"
android:text="It is a long etc......"
app:layout_constraintTop_toBottomOf="@+id/txt_one"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我需要使用两个 TextView 构建 UI,这两个 TextView 被第一个父级约束,第二个被约束在第一个 TextView 之下。当 text_one 有更多文本并将 text_two 带出屏幕时,问题就来了。期望的行为是当 text_one 有更多文本时 text_two 底部与父级对齐并且不去屏幕外。类似 weight 9:1 的东西,但这只有在 text_one 有更多文本时才需要实现。屏幕 1 显示正常行为,屏幕 2 显示问题。
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FF00"
android:text="the majority have suffered alteration in some form etc..."
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@color/colorPrimary"
android:text="It is a long established etc......"
app:layout_constraintTop_toBottomOf="@+id/txt_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以利用
app:layout_constraintHeight_max
属性 约束布局。
以编程方式使用
setMaxHeight()
在您的用例中,当屏幕渲染获取屏幕高度时,
val text1MaxHeight = screenHeight*0.9
text1.setMaxHeight(text1MaxHeight)
应该可以。
但是如果你的textview1会有更多的内容,你会希望它可以滚动,所以一个新的解决方案建议你将textview1封装在scrollView中,并在NestedScrollView中使用scrollview和textview2,如下所示
<NestedScrollView>
<ScrollView>
<TextView1/>
<ScrollView/>
<TextView2/>
<NestedScrollView>
使ScrollView中的nestedScrollingEnabled标志为false,动态设置ScrollView的maxHeight
尝试下面的示例,它保持另一个 textView
高度,即使第一个在屏幕上占据更大的 space
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FF00"
android:textSize="35sp"
android:text="the majority
e suffered alteration in some form etc..."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/txt_two"
/>
<TextView
android:id="@+id/txt_two"
android:layout_width="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="25sp"
android:background="@color/colorPrimary"
android:text="It is a long etc......"
app:layout_constraintTop_toBottomOf="@+id/txt_one"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>