约束布局中的可滚动Textview

时间:2017-09-08 10:30:56

标签: android android-scrollview android-constraintlayout

我正在制作约束布局我有三个视图。第一个是在顶部,第二个是相对于view1的位置,然后是在视图2的下方。现在在第二个视图中,我有一个应该可滚动的textview。问题是滚动视图高度覆盖全屏但我希望它有固定高度,不要将视图3移出屏幕。我不想硬编码scrollview的高度,所以知道应该是解决它的另一种方法。简而言之,无论文本长度如何,第二视图都应保留在视图1和视图3之间。

<android.support.constraint.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:layout_margin="20dp">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="48dp"
        android:layout_height="48dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/aandn_icon" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:minHeight="48dp"
        android:text="Lorem ipsum"
        app:layout_constraintLeft_toRightOf="@+id/imageView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:srcCompat="@drawable/aandn_icon" />


    <ScrollView
        android:id="@+id/scrolltext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/imageView2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView2">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <TextView
                android:id="@+id/start_tv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/sound"
                android:layout_gravity="top|left"
                android:layout_toLeftOf="@+id/sound"
                android:text="Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
Definition: A happening or sudden event that is not wanted or expected.Example: The cars collided unexpectedly on the road causing an accident.||
"
                android:textColor="#000000"
                android:textSize="20sp" />


        </android.support.constraint.ConstraintLayout>

    </ScrollView>

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrolltext"
        app:srcCompat="@drawable/aandn_icon" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:minHeight="48dp"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "
        app:layout_constraintLeft_toRightOf="@+id/imageView4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView4" />

</android.support.constraint.ConstraintLayout>

这是输出

enter image description here

2 个答案:

答案 0 :(得分:1)

您希望将textView4粘贴到屏幕底部,让ScrollView填充在TextView之上但低于顶部textView4的位置。如果我的理解是正确的,这是一种方法:

<ImageView android:id="@+id/imageView4" ... app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" /> 粘贴到屏幕底部:

ScrollView

这是图片视图,但文本视图与之相关联。

使用<ScrollView android:id="@+id/scrolltext" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@id/imageView4" app:layout_constraintLeft_toRightOf="@+id/imageView2" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="@+id/imageView2" app:layout_constraintVertical_bias="0.0"> 填充中心部分:

MATCH_CONSTRAINTS

滚动视图的高度设置为onchange(0dp),顶部和底部约束滚动视图的关联图像视图和底部图像视图。这将使滚动视图在这两个约束之间居中,因此需要垂直偏差才能将滚动视图移动到范围的顶部。

答案 1 :(得分:0)

ConstraintLayout允许您使用平面视图层次结构创建大型复杂布局(无嵌套视图组)。 @

<android.support.constraint.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"                                            
android:layout_margin="20dp">

<ImageView
 android:id="@+id/imageView"
 android:layout_width="48dp"
 android:layout_height="48dp"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:srcCompat="@drawable/ic_launcher"/>

<TextView
 android:id="@+id/textView"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_marginLeft="16dp"
 android:layout_marginStart="16dp"
 android:minHeight="48dp"
 android:text="Lorem ipsum"
 app:layout_constraintLeft_toRightOf="@+id/imageView"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="@+id/imageView"/>

<ImageView
 android:id="@+id/imageView2"
 android:layout_width="48dp"
 android:layout_height="48dp"
 android:layout_marginTop="16dp"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/textView"
 app:srcCompat="@drawable/ic_launcher"/>


<ScrollView
 android:id="@+id/scrolltext"
 android:layout_width="0dp"
 android:layout_height="0dp"
 app:layout_constraintBottom_toTopOf="@+id/textView4"
 app:layout_constraintHeight_default="wrap"
 app:layout_constraintLeft_toRightOf="@+id/imageView2"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="@+id/imageView2"
 app:layout_constraintVertical_bias="0.0">
    <TextView
     android:id="@+id/start_tv4"
     android:layout_width="wrap_content"
     android:layout_height="0dp"
     android:layout_gravity="top|start"
     android:text="@string/huge_text"
     android:textColor="#000000"
     android:textSize="20sp"
     app:layout_constraintBottom_toTopOf="@+id/textView4"
     app:layout_constraintHeight_default="wrap"/>

</ScrollView>

<ImageView
 android:id="@+id/imageView4"
 android:layout_width="48dp"
 android:layout_height="48dp"
 android:layout_marginTop="16dp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/scrolltext"
 app:srcCompat="@drawable/ic_launcher"/>

<TextView
 android:id="@+id/textView4"
 android:layout_width="0dp"
 android:layout_height="0dp"
 android:layout_marginLeft="16dp"
 android:layout_marginStart="16dp"
 android:minHeight="48dp"
 android:text="@string/lorem"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintHeight_default="wrap"
 app:layout_constraintLeft_toRightOf="@+id/imageView4"
 app:layout_constraintRight_toRightOf="parent"
 />

休息可以计算出来。

相关问题