android ScrollView超出LinearLayout的界限

fhg3lkii  于 2022-12-09  发布在  Android
关注(0)|答案(2)|浏览(184)

Udacity的课程Developing Android Apps with Kotlin(布局部分)为activity_main.xml提供了大致如下的内容:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout 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="wrap_content"
    android:orientation="vertical"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding">

    <TextView
        android:id="@+id/textView"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name_text" />

    <ImageView
        android:id="@+id/star_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/star_description"
        app:srcCompat="@android:drawable/btn_star_big_on"
        tools:ignore="ImageContrastCheck" />

    <ScrollView
        android:id="@+id/bio_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/bio_text"
            style="@style/NameStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:text="@string/bio" />
    </ScrollView>
</android.widget.LinearLayout>

目的是显示名称、名称下方的星星以及名称下方的可卷动描述。当文字足够短,不需要卷动就能容纳时,外观如下:

但是当文本足够长而需要滚动时,可滚动区域似乎会将其他组件移出视图:

课程有点旧了,所以可能有些东西已经改变了,或者可能我应用的东西不正确。需要改变什么才能让ScrollView保持在它的范围内?

smtd7mpg

smtd7mpg1#

尝试与此布局,可能你会发现它根据您的要求.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="@dimen/_20sdp"
        android:text="Your name here" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/star_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@id/textView"
        app:layout_constraintEnd_toEndOf="@id/textView"
        app:layout_constraintTop_toBottomOf="@id/textView"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/star_image"
        android:layout_marginTop="@dimen/_20sdp">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingMultiplier="2"
                android:text="set your text here" />
        </androidx.appcompat.widget.LinearLayoutCompat>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
dhxwm5r4

dhxwm5r42#

Copy and paste this
   <?xml version="1.0" encoding="utf-8"?>
        <android.widget.LinearLayout 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="wrap_content"
            android:orientation="vertical"
            android:paddingStart="@dimen/padding"
            android:paddingEnd="@dimen/padding">
        
           
        
            <ScrollView
                android:id="@+id/bio_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
       < RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         <TextView
                android:id="@+id/textView"
                style="@style/NameStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/name_text" />
        
            <ImageView
                android:id="@+id/star_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:contentDescription="@string/star_description"
                app:srcCompat="@android:drawable/btn_star_big_on"
                tools:ignore="ImageContrastCheck" />
                <TextView
                    android:id="@+id/bio_text"
                    style="@style/NameStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:lineSpacingMultiplier="1.2"
                    android:text="@string/bio" />
    </RelativeLayout>
            </ScrollView>
        </android.widget.LinearLayout>

相关问题