java - 更改方向会删除 UI 元素

标签 java android android-layout android-listview android-scrollview

当手机处于纵向模式时,它会在顶部显示标题和图像,下面是按钮和带有注释的 ListView。但是,当我将手机转为横向模式时,按钮和 ListView 消失,仅显示标题和图像。我基本上无法再滚动到图像下方了。

还有第三个元素 - 正文的 TextView - 也在 ScrollView 内部:每个布局都将具有该 TextView 包含内容或图像。当 TextView 有内容但没有图像时,也会发生同样的情况。但当图像或文本没有占据整个屏幕时,这种情况就不会发生。

肖像模式:

enter image description here

横向模式:

enter image description here

附带代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.vannevel.redditapp.activities.PostActivity">

    <TextView
        android:id="@+id/postDetailTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="#000" />

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

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

            <ImageView
                android:id="@+id/postDetailImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/postDetailBody"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/postDetailViewOnlineButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onViewOnlineButtonClicked"
        android:text="View original source" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="COMMENTS" />

    <ListView
        android:id="@+id/postDetailCommentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

最佳答案

所以你的问题是 ScrollView 中的项目比横向屏幕高度高。即使您在 ScrollView 内滚动,一旦滚动到末尾,您就无能为力。放置在 ScrollView 外部的所有元素都将离开屏幕并放置在任何可滚动容器外部。

我可以看到 3 种可能的解决方案:

1 - 为 ScrollView 设置固定大小,这样它就不会占据整个屏幕,这样您就可以让其他 View 进入。但是,这不切实际:横向手机意味着高度短,您必须适合图片并列出评论...

2 - 横向布局不同 - 图片在左边,列表和评论在右边

3 - 摆脱 ScrollView 并将 ListView 上方的所有内容设置为 ListView header (addHeaderView ) 例如:

第 1 步 - 提取 ListView 上的所有内容并将其放入不同的布局文件中 - 我们将其命名为 header.xml

第 2 步 - 膨胀布局:

View headerView = inflater.inflate(R.layout.header, listView, false);

第 3 步 - 像现在一样初始化标题上的项目

第 4 步 - 将 View 设置为列表的标题ListView:

listView.addHeaderView(headerView, null, false);

这种方法的副作用是,标题现在是 ListView 的一部分,它会随着您滚动列表而滚动。此外,它还会将您在适配器中的位置偏移 1。

关于java - 更改方向会删除 UI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126788/

相关文章:

java - 为什么 java.util.HashMap 中的支持表索引对于两个不同的提供键可以相同?

android - 关闭数据库连接的最佳位置

Android - Fedora - 安装 32 位软件包

android - Android 中的 RecyclerView 在没有任何错误消息的情况下无法正常工作

android - 为什么 margin 在 Android 中覆盖 marginLeft(与 radius 等相同)?

java - 覆盖 JFrame 的最小化

java - 我可以从 post 和 put 请求中排除字段,但将其暴露在 spring-data-rest 中的 get 中吗?

android - 为带轮廓的 Material Components 按钮的背景着色

java - JSR-303 类型 java 字段验证是否始终/隐式在字段验证之前运行?

java - 权限被拒绝导致 "Installation failed due to invalid APK file"