使用ConstraintLayout Kotlin,视图从屏幕上消失。

问题描述 投票:0回答:1

我需要用两个TextViews来构建用户界面,这两个TextViews被限制在第一个TextViews的上方,第二个TextViews被限制在第一个TextViews的下方。问题是当 text_one 有更多的文字和带来 text_two 画面外。所需的行为是当 text_one 文字较多 text_two 底部对准父版,并且不要超出屏幕。比如权重9:1,但这需要在以下情况下才能实现 text_one 有更多的文字。正常的行为表现在屏幕1,问题表现在屏幕2。

screen1 enter image description here

    <?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>
android android-layout kotlin android-constraintlayout
1个回答
1
投票

试试下面的例子,它是保持其他的例子 textView 高度,即使第一项在屏幕上占的空间较大

<?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>

0
投票

您可以利用

 app:layout_constraintHeight_max 

属性的约束布局。

在程序上使用

setMaxHeight()

在你的用例中,而屏幕渲染得到屏幕高度。

val text1MaxHeight = screenHeight*0.9
text1.setMaxHeight(text1MaxHeight)

应该可以。

但是如果你的textview1有更多的内容,你会希望它是可滚动的,所以一个新的解决方案是建议你把textview1封装在scrollView中,然后把scrollview和textview2放在NestedScrollView中,就像下面这样。

<NestedScrollView>
    <ScrollView>
      <TextView1/>
    <ScrollView/>
    <TextView2/>
<NestedScrollView>

在ScrollView中设置nestedScrollingEnabled标志为false,动态设置ScrollView的最大高度。

© www.soinside.com 2019 - 2024. All rights reserved.