Android-NestedScrollView不滚动

时间:2018-07-10 08:14:19

标签: android android-layout

我对xml有问题。当我在android studio中创建NestedScrollView时,它不会在xml预览中滚动。这很重要,因为如果看不到预览,就看不到我的作品。 另外,当我启动该应用程序时,我会看到该版本不再应有的最新版本,因为它已被该代码替换。

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="mn.cdr.du.farnext.insertionActivity.InsertionActivity"
    tools:showIn="@layout/activity_insertion"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    >

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

        <LinearLayout
            android:id="@+id/icon_static"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/carouselView"
            android:paddingBottom="20dp"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:paddingTop="20dp">

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

                <ImageView
                    android:id="@+id/cash_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_paper_bill" />

                <TextView
                    android:id="@+id/cash_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/cash_icon"
                    android:text="100€" />
            </LinearLayout>

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

                <ImageView
                    android:id="@+id/bed_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_airline_seat_individual_suite_black_24dp" />

                <TextView
                    android:id="@+id/bed_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/bed_icon"
                    android:layout_marginLeft="10dp"
                    android:paddingBottom="5px"
                    android:text="4" />
            </LinearLayout>

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

                <ImageView
                    android:id="@+id/people_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_people_black_24dp" />

                <TextView
                    android:id="@+id/people_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="200" />

            </LinearLayout>

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

                <ImageView
                    android:id="@+id/rating_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_star_black_24dp" />

                <TextView
                    android:id="@+id/rating_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3.5/5" />
            </LinearLayout>

        </LinearLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/card_contact"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/icon_static">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/ic_call_black_24dp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="5dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/contact_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Phone"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/contact_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="+39 1234567890" />
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/card_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/card_contact"
            android:layout_marginTop="10dp">

            <ImageView
                android:id="@+id/address_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/ic_place_black_24dp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="5dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Address"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/address_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="La Farneta, 56048 Province of Pisa" />
            </LinearLayout>

        </android.support.v7.widget.CardView>

        <TextView
            android:id="@+id/txt_place_card"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/card_address"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txt_place_card"
            android:text="Services" />

    </RelativeLayout>

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

4 个答案:

答案 0 :(得分:1)

只需在xml中更改此代码 NestedScrollView的高度始终为match_parent

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="mn.cdr.du.farnext.insertionActivity.InsertionActivity"
    tools:showIn="@layout/activity_insertion"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

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

 ....
 ....
     ....

</RelativeLayout>

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

答案 1 :(得分:0)

NestedScrollView的高度应为match_parent。并且其子级(RelativeLayout)的高度应为wrap_content

答案 2 :(得分:0)

ScrollViews通常不会在XML预览中滚动。您只能在设备上看到它们。这就是为什么将Android手机连接到系统以进行实时测试总是更好的原因。

那样,您可以轻松地纠正任何错误

答案 3 :(得分:0)

这不是NestedScrollView的高度问题

新版Android Studio在布局编辑器中滚动ScrollView时出现问题。 在旧版本中,ScrollView在编辑器中具有流畅的滚动行为。这是Android Studio的错误。

我通常会采用的解决方案

  • 您可以在布局编辑器底部看到设计文本。单击设计,在那里您将可以滚动。我每次都可以正常工作。
  • 如果要使文本编辑器可滚动,请重新启动IDE(File>Invalidate Cache/ Restart>Just Restart)。我的文本编辑器通过此技巧开始工作。
  • 一种变通方法:您可以暂时使上方视图不可见,以查看底部视图。