【问题标题】:Wrap content not working and scroll stopped working包装内容不起作用并且滚动停止工作
【发布时间】:2018-08-03 16:05:40
【问题描述】:

包装内容不起作用并且滚动停止工作。

您好,我正在为 Android 开发一个应用程序,我注意到我的应用程序在智能手机上运行良好,但是在纵向模式的平板电脑中它增加了巨大的空间并且 ScrollView 停止工作。

智能手机的看法:

平板电脑的视图:

我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/descAbout"
            android:layout_marginBottom="10dp"
            android:textSize="16sp"
            android:layout_marginTop="10dp" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true">
                <ImageView
                    android:id="@+id/imgSpaceApps"
                    android:layout_width="wrap_content"
                    android:layout_height="115dp"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center"
                    android:src="@drawable/spaceApps" />
                <ImageView
                    android:id="@+id/imgNasaLogo"
                    android:layout_width="wrap_content"
                    android:layout_height="115dp"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center"
                    android:src="@drawable/nasaLogo" />
            </LinearLayout>
        </RelativeLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="@string/lblContact2"
            android:layout_marginBottom="5dp" />
        <TextView
            android:id="@+id/lblOurEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="@string/lblContactUs"
            android:clickable="true"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="@string/lblVisit"
            android:autoLink="web"
            android:layout_marginBottom="5dp" />
        <TextView
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="@string/lblSpecialGreeting"
            android:layout_marginBottom="5dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url7"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url8"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url6"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url5"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url1"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url2"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url3"
            android:layout_marginBottom="10dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url4"
            android:layout_marginBottom="10dp" />
        <TextView
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="@string/lblPPrivacy"
            android:layout_marginBottom="5dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:autoLink="web"
            android:text="@string/url9"
            android:layout_marginBottom="10dp" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:adjustViewBounds="true"
            android:layout_gravity="center"
            android:src="@drawable/supernova" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text="@string/About6"
            android:layout_marginBottom="10dp" />
    </LinearLayout>
</ScrollView>

如果我删除以下属性:

android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"

结果是一样的:

我在 Android 6 和 7 中测试,最低 SDK 为 21。

有人遇到过这种情况吗?感谢您的帮助。

【问题讨论】:

  • 不是解决方案;但原因是:这是因为您有一个带有height=wrap_content 的RelativeLayout 和一个带有alignParentBottom=true 的子视图。这使得 RelativeLayout 填充所有可用空间并将子级放置在底部。这是整个屏幕,因为滚动视图有fillviewport=true。使用 RelativeLayout 时,这可能会有些烦人的行为。您将不得不重新考虑您的布局结构或更改其中一些属性。
  • 嗨@RobCo,它不起作用:ibb.co/gUEQxc
  • 抱歉,我无法用您的代码重现该错误。使用 Android Studio 3.3 以及一些 API 级别 22 和 24 的平板电脑模拟器

标签: android xml scrollview tablet android-relativelayout


【解决方案1】:

正如我在评论中提到的,原因是保存图像的 RelativeLayout。 与 'wrap_content' 一起使用时,RelativeLayout 可能会导致意外结果,它实际上会尝试匹配其父级并在边缘布置子级。

在这种情况下,RelativeLayout 无论如何都没有用;您可以将其删除并稍微更改子 LinearLayout 的属性:

        ...
        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            >
            <ImageView
                android:id="@+id/imgSpaceApps"
                android:layout_width="wrap_content"
                android:layout_height="115dp"
                android:adjustViewBounds="true"
                android:src="@drawable/spaceApps" />
            <ImageView
                android:id="@+id/imgNasaLogo"
                android:layout_width="wrap_content"
                android:layout_height="115dp"
                android:adjustViewBounds="true"
                android:src="@drawable/nasaLogo" />
        </LinearLayout>
        ...

【讨论】:

    【解决方案2】:

    改成这样:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/descAbout"
                android:layout_marginBottom="10dp"
                android:textSize="16sp"
                android:layout_marginTop="10dp" />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_centerHorizontal="true">
                    <ImageView
                        android:id="@+id/imgSpaceApps"
                        android:layout_width="wrap_content"
                        android:layout_height="115dp"
                        android:adjustViewBounds="true"
                        android:layout_gravity="center"
                        android:src="@drawable/spaceApps" />
                    <ImageView
                        android:id="@+id/imgNasaLogo"
                        android:layout_width="wrap_content"
                        android:layout_height="115dp"
                        android:adjustViewBounds="true"
                        android:layout_gravity="center"
                        android:src="@drawable/nasaLogo" />
                </LinearLayout>
            </RelativeLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/lblContact2"
                android:layout_marginBottom="5dp" />
            <TextView
                android:id="@+id/lblOurEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/lblContactUs"
                android:clickable="true"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/lblVisit"
                android:autoLink="web"
                android:layout_marginBottom="5dp" />
            <TextView
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/lblSpecialGreeting"
                android:layout_marginBottom="5dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url7"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url8"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url6"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url5"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url1"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url2"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url3"
                android:layout_marginBottom="10dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url4"
                android:layout_marginBottom="10dp" />
            <TextView
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/lblPPrivacy"
                android:layout_marginBottom="5dp" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:autoLink="web"
                android:text="@string/url9"
                android:layout_marginBottom="10dp" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:layout_marginBottom="5dp"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                android:src="@drawable/supernova" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:textSize="14sp"
                android:text="@string/About6"
                android:layout_marginBottom="10dp" />
        </LinearLayout>
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多