ScrollView没有按片段滚动或无法正常工作

时间:2018-06-27 06:00:08

标签: java android android-layout android-fragments

我在android领域还很陌生,我正在开发一个应用程序,但是我遇到一个错误,这使我发疯。问题是它不能完全滚动,它使屏幕上没有按钮,因此无法看到它。 Scrollview在每个活动中都可以正常工作,但在选项卡式活动中不起作用。 如果我将高度match-parent更改为500dp或任何整数值,则此代码android:layout_height="match_parent"会产生问题。我想知道为什么配对父母不起作用。

我已经尝试过对这个问题进行所有建议的修改,但仍然没有任何修改效果。

这是我的XML文件

<RelativeLayout 
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:background="@mipmap/back"
android:paddingBottom="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
tools:context="com.example.sahil.bloodbank.first"
>

<ScrollView
    android:id="@+id/sc1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="20sp"
    android:text="Welcome,"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/wall1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:gravity="center"
        android:text="@string/data"
        android:textAlignment="gravity"
        android:textColor="@color/data"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/note"
        android:textColor="@color/data"
        android:textStyle="bold"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/desc"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="I want to Donate Blood"
        android:textColor="@color/text"
        android:textSize="18sp"
        android:layout_below="@+id/data"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="14dp"
        />

</LinearLayout>
</ScrollView>
</RelativeLayout>

6 个答案:

答案 0 :(得分:0)

滚动视图必须是根元素。

<ScrollView 
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"
tools:context="com.example.sahil.bloodbank.first"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/back"
android:paddingBottom="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="20sp"
    android:text="Welcome,"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/wall1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:gravity="center"
        android:text="@string/data"
        android:textAlignment="gravity"
        android:textColor="@color/data"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/note"
        android:textColor="@color/data"
        android:textStyle="bold"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/desc"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="I want to Donate Blood"
        android:textColor="@color/text"
        android:textSize="18sp"
        android:layout_below="@+id/data"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="14dp"
        />

</LinearLayout>

</RelativeLayout>
</ScrollView>

答案 1 :(得分:0)

您的滚动视图必须是MATCHPARENT的高度

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

答案 2 :(得分:0)

将您的Scrollview设为根视图,高度应为MATCH_PARENT

<ScrollView 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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20sp"
        android:text="Welcome,"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/wall1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView"
            android:gravity="center"
            android:text="@string/data"
            android:textAlignment="gravity"
            android:textColor="@color/data"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/note"
            android:textColor="@color/data"
            android:textStyle="bold"
            android:layout_marginTop="40dp"
            android:layout_below="@+id/desc"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:id="@+id/donate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="I want to Donate Blood"
            android:textColor="@color/text"
            android:textSize="18sp"
            android:layout_below="@+id/data"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="14dp"
            />
        </LinearLayout>
    </ScrollView>

答案 3 :(得分:0)

尝试此代码并更改此代码,以增强您的需求,并确保在发生滚动效果后视图的大小达到屏幕尺寸。

constructor(private router: Router) {}

this.router.navigate(['/gvpn']); 

答案 4 :(得分:0)

您不需要将 Scroll View 用作 Root Layout 。但是您可能需要向{em> Scroll View 添加更多属性,例如android:scrollbars="vertical"android:fillViewport="true"。同样,您也可以使用将Button带到Scroll View之外并仅滚动其他内容的方法,这样您的Button总是在屏幕上可见,只有您的内容会滚动。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@mipmap/ic_launcher"
    android:paddingBottom="10dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="5dp">

    <ScrollView
        android:layout_above="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Welcome,"
                android:textColor="#000000"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                app:srcCompat="@android:drawable/ic_menu_share" />

            <TextView
                android:id="@+id/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView"
                android:gravity="center"
                android:text="Description DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription"
                android:textAlignment="gravity"
                android:textColor="@color/colorPrimary"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/desc"
                android:layout_marginTop="40dp"
                android:text=" Note DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote Data"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />



        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:background="@color/colorPrimary"
        android:text="I want to Donate Blood"
        android:textColor="@color/colorPrimary"
        android:textSize="18sp" />
</RelativeLayout>

Fragment内部,您可以在android:isScrollContainer="false"内部尝试使用此属性Scroll View

如果这不起作用,请尝试使用NestedScrollView中的Fragment

<android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true"
     app:layout_behavior="@string/appbar_scrolling_view_behavior" >


</android.support.v4.widget.NestedScrollView>

答案 5 :(得分:0)

需要将标准ScrollView用作父级。我认为您在活动中使用ToolBar。 因此,在这种情况下,您可以将NestedScrollView与属性 app:layout_behavior =“ @ string / appbar_scrolling_view_behavior”

因此将ScrollView替换为:---

<NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fillViewport="true"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  {Your Child View}

</android.support.v4.widget.NestedScrollView>