繁体   English   中英

Android-NestedScrollView不滚动

[英]Android - NestedScrollView doesn't scroll

我对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>

只需在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>

NestedScrollView的高度应为match_parent 并且它的子级( RelativeLayout )应该具有高度wrap_content

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

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

这不是NestedScrollView的高度问题

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

我通常会做的解决方案

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM