【问题标题】:ScrollView can't scroll in an Android appScrollView 无法在 Android 应用中滚动
【发布时间】:2022-01-23 23:16:45
【问题描述】:

我用ScrollView做根,然后加LinearLayout,试了几次,就是不能滚动,我试着把ScrollView里的android:layout_height="match_parent"改成wrap_content,用LinearLayout做根,然后加了ScrollView,然后把LinearLayout放到ScrollView里面,但是还是不能滚动,不知道是什么问题,在我的其他app里ScrollView在工作。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/intro_background"
    android:fillViewport="true"
    tools:context=".IntroActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">
    <TextView
        android:id="@+id/tv_app_name_intro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/intro_screen_title_text_marginTop"
        android:text="@string/app_name"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/intro_screen_title_text_size"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/intro_screen_image_marginTop"
        android:contentDescription="@string/image_contentDescription"
        android:src="@mipmap/ic_task_image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/lets_get_started_text_marginTop"
        android:text="@string/let_s_get_started_text"
        android:textColor="@color/primary_text_color"
        android:textSize="@dimen/lets_get_started_text_size" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/intro_text_marginStartEnd"
        android:layout_marginTop="@dimen/intro_text_marginTop"
        android:layout_marginEnd="@dimen/intro_text_marginStartEnd"
        android:gravity="center"
        android:lineSpacingExtra="@dimen/intro_text_line_spacing_extra"
        android:text="@string/intro_text_description"
        android:textColor="@color/secondary_text_color"
        android:textSize="@dimen/intro_text_size" />

    <Button
        android:id="@+id/btn_sign_in_intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/btn_marginStartEnd"
        android:layout_marginTop="@dimen/intro_screen_sign_in_btn_marginTop"
        android:layout_marginEnd="@dimen/btn_marginStartEnd"
        android:background="@drawable/shape_button_rounded"
        android:foreground="?attr/selectableItemBackground"
        android:gravity="center"
        android:paddingTop="@dimen/btn_paddingTopBottom"
        android:paddingBottom="@dimen/btn_paddingTopBottom"
        android:text="@string/sign_in"
        android:textColor="@android:color/white"
        android:textSize="@dimen/btn_text_size" />

    <Button
        android:id="@+id/btn_sign_up_intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/btn_marginStartEnd"
        android:layout_marginTop="@dimen/intro_screen_sign_up_btn_marginTop"
        android:layout_marginEnd="@dimen/btn_marginStartEnd"
        android:background="@drawable/white_border_shape_button_rounded"
        android:foreground="?attr/selectableItemBackground"
        android:gravity="center"
        android:paddingTop="@dimen/btn_paddingTopBottom"
        android:paddingBottom="@dimen/btn_paddingTopBottom"
        android:text="@string/sign_up"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/btn_text_size" />
</LinearLayout>
</ScrollView>  

我试图将 ScrollView 放在 LinearLayout 中,但它不起作用。代码如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/intro_background"
    android:orientation="vertical"
    tools:context=".IntroActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:padding="16dp">
            <TextView
                android:id="@+id/tv_app_name_intro"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/intro_screen_title_text_marginTop"
                android:text="@string/app_name"
                android:textColor="@color/colorAccent"
                android:textSize="@dimen/intro_screen_title_text_size"
                android:textStyle="bold" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/intro_screen_image_marginTop"
                android:contentDescription="@string/image_contentDescription"
                android:src="@mipmap/ic_task_image" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/lets_get_started_text_marginTop"
                android:text="@string/let_s_get_started_text"
                android:textColor="@color/primary_text_color"
                android:textSize="@dimen/lets_get_started_text_size" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/intro_text_marginStartEnd"
                android:layout_marginTop="@dimen/intro_text_marginTop"
                android:layout_marginEnd="@dimen/intro_text_marginStartEnd"
                android:gravity="center"
                android:lineSpacingExtra="@dimen/intro_text_line_spacing_extra"
                android:text="@string/intro_text_description"
                android:textColor="@color/secondary_text_color"
                android:textSize="@dimen/intro_text_size" />

            <Button
                android:id="@+id/btn_sign_in_intro"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="@dimen/btn_marginStartEnd"
                android:layout_marginTop="@dimen/intro_screen_sign_in_btn_marginTop"
                android:layout_marginEnd="@dimen/btn_marginStartEnd"
                android:background="@drawable/shape_button_rounded"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:paddingTop="@dimen/btn_paddingTopBottom"
                android:paddingBottom="@dimen/btn_paddingTopBottom"
                android:text="@string/sign_in"
                android:textColor="@android:color/white"
                android:textSize="@dimen/btn_text_size" />

            <Button
                android:id="@+id/btn_sign_up_intro"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="@dimen/btn_marginStartEnd"
                android:layout_marginTop="@dimen/intro_screen_sign_up_btn_marginTop"
                android:layout_marginEnd="@dimen/btn_marginStartEnd"
                android:background="@drawable/white_border_shape_button_rounded"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:paddingTop="@dimen/btn_paddingTopBottom"
                android:paddingBottom="@dimen/btn_paddingTopBottom"
                android:text="@string/sign_up"
                android:textColor="@color/colorAccent"
                android:textSize="@dimen/btn_text_size" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

问题不是我如何使用ScrollView,问题是android:foreground="?attr/selectableItemBackground"

显示警告,属性 android:foreground 对低于 23 的 API 级别没有影响(当前最小值为 21),然后我在 layout-v23 中单击了覆盖资源,因此 Android Studio 生成了一个 xml 文件,activity_intro.xml (v23 ),我需要同时更改 activity_intro.xml (v23) 和 activity_intro.xml,然后才能正常工作。

【问题讨论】:

  • 我认为您的滚动视图是正确的,但在线性布局子项中存在问题。
  • 什么问题?我看不到问题。

标签: android


【解决方案1】:

对于ScrollView来说,滚动内容内的内容应该大于ScrollView的高度。您将 ScrollView 保持为 match_parent 是正确的,但我认为几个 Button 或 TextView 不足以使其滚动。对于测试,您可以尝试将其中一个视图的高度设置为非常大,例如 2000dp,然后查看它是否滚动。

【讨论】:

  • 我在模拟器上测试的时候,因为不需要滚动,所以什么都显示,但是在我的真机上测试时,最后一个按钮只显示一半,不能滚动。
【解决方案2】:

最好的解决方案是使用嵌套的 androidx.core.widget.NestedScrollView

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/intro_background"
    android:fillViewport="true"
    tools:context=".IntroActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">
    <TextView
        android:id="@+id/tv_app_name_intro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/intro_screen_title_text_marginTop"
        android:text="@string/app_name"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/intro_screen_title_text_size"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/intro_screen_image_marginTop"
        android:contentDescription="@string/image_contentDescription"
        android:src="@mipmap/ic_task_image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/lets_get_started_text_marginTop"
        android:text="@string/let_s_get_started_text"
        android:textColor="@color/primary_text_color"
        android:textSize="@dimen/lets_get_started_text_size" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/intro_text_marginStartEnd"
        android:layout_marginTop="@dimen/intro_text_marginTop"
        android:layout_marginEnd="@dimen/intro_text_marginStartEnd"
        android:gravity="center"
        android:lineSpacingExtra="@dimen/intro_text_line_spacing_extra"
        android:text="@string/intro_text_description"
        android:textColor="@color/secondary_text_color"
        android:textSize="@dimen/intro_text_size" />

    <Button
        android:id="@+id/btn_sign_in_intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/btn_marginStartEnd"
        android:layout_marginTop="@dimen/intro_screen_sign_in_btn_marginTop"
        android:layout_marginEnd="@dimen/btn_marginStartEnd"
        android:background="@drawable/shape_button_rounded"
        android:foreground="?attr/selectableItemBackground"
        android:gravity="center"
        android:paddingTop="@dimen/btn_paddingTopBottom"
        android:paddingBottom="@dimen/btn_paddingTopBottom"
        android:text="@string/sign_in"
        android:textColor="@android:color/white"
        android:textSize="@dimen/btn_text_size" />

    <Button
        android:id="@+id/btn_sign_up_intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/btn_marginStartEnd"
        android:layout_marginTop="@dimen/intro_screen_sign_up_btn_marginTop"
        android:layout_marginEnd="@dimen/btn_marginStartEnd"
        android:background="@drawable/white_border_shape_button_rounded"
        android:foreground="?attr/selectableItemBackground"
        android:gravity="center"
        android:paddingTop="@dimen/btn_paddingTopBottom"
        android:paddingBottom="@dimen/btn_paddingTopBottom"
        android:text="@string/sign_up"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/btn_text_size" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

如果这不起作用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/intro_background"
    android:fillViewport="true"
    tools:context=".IntroActivity">
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:id="@+id/tv_app_name_intro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/intro_screen_title_text_marginTop"
            android:text="@string/app_name"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/intro_screen_title_text_size"
            android:textStyle="bold" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/intro_screen_image_marginTop"
            android:contentDescription="@string/image_contentDescription"
            android:src="@mipmap/ic_task_image" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/lets_get_started_text_marginTop"
            android:text="@string/let_s_get_started_text"
            android:textColor="@color/primary_text_color"
            android:textSize="@dimen/lets_get_started_text_size" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/intro_text_marginStartEnd"
            android:layout_marginTop="@dimen/intro_text_marginTop"
            android:layout_marginEnd="@dimen/intro_text_marginStartEnd"
            android:gravity="center"
            android:lineSpacingExtra="@dimen/intro_text_line_spacing_extra"
            android:text="@string/intro_text_description"
            android:textColor="@color/secondary_text_color"
            android:textSize="@dimen/intro_text_size" />

        <Button
            android:id="@+id/btn_sign_in_intro"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="@dimen/btn_marginStartEnd"
            android:layout_marginTop="@dimen/intro_screen_sign_in_btn_marginTop"
            android:layout_marginEnd="@dimen/btn_marginStartEnd"
            android:background="@drawable/shape_button_rounded"
            android:foreground="?attr/selectableItemBackground"
            android:gravity="center"
            android:paddingTop="@dimen/btn_paddingTopBottom"
            android:paddingBottom="@dimen/btn_paddingTopBottom"
            android:text="@string/sign_in"
            android:textColor="@android:color/white"
            android:textSize="@dimen/btn_text_size" />

        <Button
            android:id="@+id/btn_sign_up_intro"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="@dimen/btn_marginStartEnd"
            android:layout_marginTop="@dimen/intro_screen_sign_up_btn_marginTop"
            android:layout_marginEnd="@dimen/btn_marginStartEnd"
            android:background="@drawable/white_border_shape_button_rounded"
            android:foreground="?attr/selectableItemBackground"
            android:gravity="center"
            android:paddingTop="@dimen/btn_paddingTopBottom"
            android:paddingBottom="@dimen/btn_paddingTopBottom"
            android:text="@string/sign_up"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/btn_text_size" />
    </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</LinearLayout>

这可能行得通...

【讨论】:

  • 这两种方式都行不通。
  • 我的简单答案是使用嵌套滚动视图而不是滚动视图
猜你喜欢
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 2021-09-12
  • 1970-01-01
相关资源
最近更新 更多