Android系统。 ScrollView不会第一次滚动,但是当内容发生变化时,它就会滚动

时间:2017-02-15 20:09:21

标签: android xml android-scrollview

我在滚动视图中有我的视图,这样如果内容大于屏幕大小,用户可以向下滚动。我注意到了一件奇怪的事情。

内容第一次出现时,它不会滚动。但是,当用户更改设置并且滚动视图内的视图内容重新加载时,它会变为可滚动。

这是为什么?我的意思是明确的吗?

编辑:这只发生在我的Nexus 5X上。但是,当我使用三星J10时,它可以立即使用。

我的XML是一个相对布局,带有滚动视图的子元素,其中包含其他视图。

这是我的xml:

<?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:id="@+id/content_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.apps.reuven.egertandcohentravel.Activities.HomeActivity"
tools:showIn="@layout/activity_home">

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

    >




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


        <ProgressBar
            android:layout_width="50dp"
            android:id="@+id/progressBar"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            android:visibility="gone"
            />

        <TextView
            android:id="@+id/textViewLinkToOrder"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click here to book travel insurance."
            android:textColor="@color/colorPrimary"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:gravity="center"

            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/buttons_linear_layout"
            android:orientation="horizontal"
            android:layout_below="@id/textViewLinkToOrder">

            <Button
                android:layout_width="200dp"
                android:id="@+id/choose_country_button"
                android:onClick="onChooseCountryButtonClick"
                android:layout_height="wrap_content"
                android:text="Choose country"
                android:layout_marginLeft="5dp"
                android:background="@color/colorPrimary"
                android:layout_weight="1"
                android:textColor="#ffff"
                android:layout_marginRight="5dp"
                android:layout_below="@id/textViewLinkToOrder"
                android:layout_centerHorizontal="true"/>


            <Button
                android:layout_width="200dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary"
                android:textColor="#fff"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:id="@+id/automatic_country_button"
                android:layout_height="wrap_content"
                android:text="My Location"
                android:layout_below="@id/choose_country_button"
                android:layout_centerHorizontal="true"/>




        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:orientation="vertical"
            android:id="@+id/linearLayoutAllDetails"
            android:layout_below="@id/buttons_linear_layout"
            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="No country yet selected"
                android:gravity="center"
                android:id="@+id/textView_coumtry_name"
                android:textColor="#000000"
                android:textSize="30sp"
                android:padding="5dp"
                android:textStyle="bold"
                android:layout_weight="1" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                >

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/police"/>

                <TextView
                    android:textSize="17sp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Police"
                    android:textColor="#000000"
                    android:layout_gravity="center_vertical" />

                <TextView
                    android:layout_marginLeft="10dp"
                    android:textSize="17sp"
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/police_text_view"
                    android:text=""
                    android:textColor="#000000"
                    android:layout_gravity="center_vertical"/>

                <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    />

                <ImageView
                    android:id="@+id/police_phone_button"
                    android:layout_width="30dp"
                    android:layout_gravity="center_vertical"
                    android:layout_height="30dp"
                    android:src="@android:drawable/sym_action_call"


                    />




            </LinearLayout>



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/ambulance"/>

                <TextView
                    android:textSize="17sp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ambulance"
                    android:textColor="#000000"
                    android:layout_gravity="center_vertical" />

                <TextView
                    android:layout_marginLeft="10dp"
                    android:textSize="17sp"
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/ambulance_text_view"
                    android:text=""
                    android:textColor="#000000"
                    android:layout_gravity="center_vertical"/>

                <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    />

                <ImageView
                    android:layout_width="30dp"
                    android:layout_gravity="center_vertical"
                    android:layout_height="30dp"
                    android:src="@android:drawable/sym_action_call"
                    android:id="@+id/ambulance_phone_button"


                    />




            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/israel_consulate"/>

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

                    <TextView
                        android:textSize="17sp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Israel Consulate"
                        android:textColor="#000000"
                        android:layout_gravity="center_vertical" />

                    <TextView
                        android:layout_marginLeft="10dp"
                        android:textSize="17sp"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/israel_consulate_text_view"
                        android:text=""
                        android:textColor="#000000"
                        android:layout_gravity="center_vertical"/>



                </LinearLayout>



                <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    />

                <ImageView
                    android:layout_width="30dp"
                    android:layout_gravity="center_vertical"
                    android:layout_height="30dp"
                    android:src="@android:drawable/sym_action_call"
                    android:id="@+id/israel_phone_button"

                    />




            </LinearLayout>




            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/chabad"/>



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

                    <TextView
                        android:textSize="17sp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Beit Chabad"
                        android:textColor="#000000"
                        android:layout_gravity="center_vertical" />

                    <TextView
                        android:layout_marginLeft="10dp"
                        android:textSize="17sp"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/chabad_address_text_view"
                        android:text="3 Blue Street, USA"
                        android:textColor="#000000"
                        android:layout_gravity="center_vertical"/>


                    <TextView
                        android:layout_marginLeft="10dp"
                        android:textSize="17sp"
                        android:layout_marginRight="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/chabad_number_text_view"
                        android:text="+44 456 3245234"
                        android:textColor="#000000"
                        android:layout_gravity="center_vertical"/>



                </LinearLayout>


                <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    />

                <ImageView
                    android:layout_width="30dp"
                    android:layout_gravity="center_vertical"
                    android:layout_height="30dp"
                    android:src="@android:drawable/sym_action_call"
                    android:id="@+id/chabad_phone_button"


                    />




            </LinearLayout>





        </LinearLayout>







    </RelativeLayout>

</ScrollView>

非常感谢,我无法解决这个问题。

1 个答案:

答案 0 :(得分:0)

我也有同样的问题,通过在ScrollView中添加TableLayout解决了我的问题。然后,在TableLayout中添加您的内容(RelativeLayout)。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">

        <-- Your RelativeLayout goes here -->

    </TableLayout>
</ScrollView>

如果能解决问题,请告诉我。不要忘记将属性fillViewport = true添加到ScrollView,以便TableLayout匹配父视图的宽度和高度。