【问题标题】:ListView cut last itemListView 剪切最后一项
【发布时间】:2015-10-14 09:54:59
【问题描述】:

我有一个带有这个 xml 的片段:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/footer">
    <My.Views.NoResultPanel
        style="@style/default_no_result_panel"
        android:id="@+id/noResultPanel"
        local:textID="@string/no_chat" />
    <My.Views.LoadingPanel
        style="@style/default_loading_panel"
        android:id="@+id/loadingPanel"
        local:textID="@string/loading_chat" />
    <ListView
        android:id="@+id/lvChat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:dividerHeight="8dp"
        android:stackFromBottom="true"
        android:listSelector="@android:color/transparent" />
    <View
        style="@style/default_divider"
        android:layout_gravity="bottom" />
</FrameLayout>
    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="8dp">
        <ImageView
            android:id="@+id/imgAttachment"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_attachment_grey600_24dp"
            android:layout_alignParentLeft="true" />
        <EditText
            android:id="@+id/txtMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/imgVisibility"
            android:layout_toRightOf="@+id/imgAttachment"
            android:layout_centerVertical="true"
            android:layout_marginRight="4dp"
            android:layout_marginLeft="4dp"
            android:maxLines="3"
            android:inputType="textShortMessage"
            android:hint="@string/write_a_message_here" />
        <ImageView
            android:id="@+id/imgVisibility"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="4dp"
            android:src="@drawable/ic_eye_grey600_24dp"
            android:layout_toLeftOf="@+id/imgSend" />
        <ImageView
            android:id="@+id/imgSend"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_send_grey600_24dp"
            android:layout_alignParentRight="true" />
    </RelativeLayout>
</RelativeLayout>

我需要模拟聊天,所以我使用的是 android:stackFromBottom

问题是当我第一次打开那个片段时,我发现最后一个项目“被剪切”了。 这是片段的截图。

我做错了什么?

更新代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:padding="8dp">
    <ImageView
        android:id="@+id/imgAttachment"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="8dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_attachment_grey600_24dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/Default_clicked_view_rounded" />
    <EditText
        android:id="@+id/txtMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/imgVisibility"
        android:layout_toRightOf="@+id/imgAttachment"
        android:layout_centerVertical="true"
        android:layout_marginRight="4dp"
        android:layout_marginLeft="4dp"
        android:maxLines="3"
        android:inputType="textShortMessage"
        android:hint="@string/write_a_message_here" />
    <ImageView
        android:id="@+id/imgVisibility"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="8dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="4dp"
        android:src="@drawable/ic_eye_grey600_24dp"
        android:layout_toLeftOf="@+id/imgSend"
        android:background="@drawable/Default_clicked_view_rounded" />
    <ImageView
        android:id="@+id/imgSend"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="8dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_send_grey600_24dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/Default_clicked_view_rounded" />
</RelativeLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/footer">
    <My.Views.NoResultPanel
        style="@style/default_no_result_panel"
        android:id="@+id/noResultPanel"
        local:textID="@string/no_chat" />
    <My.Views.LoadingPanel
        style="@style/default_loading_panel"
        android:id="@+id/loadingPanel"
        local:textID="@string/loading_chat" />
    <ListView
        android:id="@+id/lvChat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:dividerHeight="8dp"
        android:stackFromBottom="true"
        android:listSelector="@android:color/transparent" />
    <View
        style="@style/default_divider"
        android:layout_gravity="bottom" />
</FrameLayout>
</RelativeLayout> 

【问题讨论】:

  • 尝试删除android:stackFromBottom="true"

标签: android android-fragments android-listview


【解决方案1】:
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"

你的 FrameLayout 是匹配父级的,所以它首先覆盖整个屏幕,所以不确定它下面的新布局@+id/footer...在它的顶部你有覆盖底部的 RelativeLayout 这是页脚所以只需更改你的布局到。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
  //your Footer Layout will always stay in bottom
<RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="8dp">
        <ImageView
            android:id="@+id/imgAttachment"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_attachment_grey600_24dp"
            android:layout_alignParentLeft="true" />
        <EditText
            android:id="@+id/txtMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/imgVisibility"
            android:layout_toRightOf="@+id/imgAttachment"
            android:layout_centerVertical="true"
            android:layout_marginRight="4dp"
            android:layout_marginLeft="4dp"
            android:maxLines="3"
            android:inputType="textShortMessage"
            android:hint="@string/write_a_message_here" />
        <ImageView
            android:id="@+id/imgVisibility"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="4dp"
            android:src="@drawable/ic_eye_grey600_24dp"
            android:layout_toLeftOf="@+id/imgSend" />
        <ImageView
            android:id="@+id/imgSend"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_send_grey600_24dp"
            android:layout_alignParentRight="true" />
    </RelativeLayout>


//your FrameLayout now will adjust above footer
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/footer">     --------change from +id to id now
    <My.Views.NoResultPanel
        style="@style/default_no_result_panel"
        android:id="@+id/noResultPanel"
        local:textID="@string/no_chat" />
    <My.Views.LoadingPanel
        style="@style/default_loading_panel"
        android:id="@+id/loadingPanel"
        local:textID="@string/loading_chat" />
    <ListView
        android:id="@+id/lvChat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:dividerHeight="8dp"
        android:stackFromBottom="true"
        android:listSelector="@android:color/transparent" />
    <View
        style="@style/default_divider"
        android:layout_gravity="bottom" />
</FrameLayout>

</RelativeLayout>

更新: 请尝试将android:transcriptMode="alwaysScroll" 属性添加到listView

设置列表的转录模式。在脚本模式下,列表会滚动到底部,以便在添加新项目时显示它们。这样,列表将自动滚动到底部。

必须是您引用的以下常量值之一doc

Reference

【讨论】:

  • 不幸的是,这并没有解决问题。我更新了这个更新的代码的开放帖子
  • 您是否尝试过上述解决方案并为 Framlayout 提供底部边距
  • 尝试使用 android:layout_marginBottom="56dp" 但问题仍然存在
  • 这成功了:) 谢谢!你知道为什么需要这个属性吗?
  • @user3471528 我添加了原因和参考以获取更多信息,因此您也可以尝试使用它的其他值
【解决方案2】:

用垂直方向的LinearLayout替换你的父RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weight="1"
        android:layout_above="@+id/footer">
        ....
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
        ....
    </RelativeLayout>

</LinearLayout>

FrameLayout 将匹配父级高度

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多