【问题标题】:How to make a button's location movable based on the number of items in the ListView如何根据 ListView 中的项目数使按钮的位置可移动
【发布时间】:2014-02-12 07:11:30
【问题描述】:

在布局文件中,我有一个大小可以动态增长/缩小的 Listview。我有一个按钮 btn_rec_add,它是单击事件,我在 ListView 中添加了一个项目。我在布局文件中尝试了许多更改,但无法根据 ListView 中的项目数使按钮移动其位置。如果我将按钮保留在具有 ListView 的相同 RelativeLayout 中,那么按钮会动态移动,这正是我想要的,但在 4.3 英寸显示屏手机中添加 5 个或更多元素后我看不到按钮。如果我将按钮保留在 ListView 的 RelativeLayout 之外,则它会固定在屏幕上。

目前,btn_rec_add 固定在布局的底部。谁能帮我解决这个问题。

这是 XML 代码:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >

<ImageView
    android:id="@id/top_bar_view"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/top_bar"
    android:contentDescription="@string/content" />

<TextView
    android:id="@+id/txt_recipients"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="8dp"
    android:padding="8dp"
    android:text="@string/text_recipients"
    android:textColor="#FFFFFF"
    android:textSize="16sp" />

<ImageButton
    android:id="@id/btn_back"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/content"
    android:paddingTop="6dp"
    android:src="@drawable/ic_back" />

<RelativeLayout
    android:id="@+id/Rlayout_recipients"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/btn_rec_add"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/top_bar_view" >

    <ListView
        android:id="@+id/rec_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:paddingTop="20dp" />
</RelativeLayout>

<ImageButton
    android:id="@+id/btn_rec_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/content"
    android:src="@drawable/icon_add" />

</RelativeLayout>

【问题讨论】:

    标签: android xml android-listview android-linearlayout android-relativelayout


    【解决方案1】:

    如果我理解正确,您希望按钮的行为如下:

    • 如果ListView 未扩展到填满屏幕,则显示在最后一个ListView 项目下方
    • 如果ListView 扩展了屏幕的整个高度,则按钮应位于屏幕底部,但列表应保持可滚动状态

    如果我的理解是正确的,您应该将您的ListView 和您的按钮放在LinearLayout 中,如下所示:

    <LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
        <ListView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <Button 
            android:layout_width="match_parent" 
            android:layout_height="@dimen/button_height"
            android:background="@drawable/button_image" />
    
    </LinearLayout>
    

    以上布局效果如下:

    • 垂直放置项目的布局
    • 与父级一样宽但与ListViewButton 一样高的布局
    • ListView 将占据布局中按钮未占用的所有空间(这是layout_weight="1",而Button 没有布局权重,因此它只会填充所需的空间,如本例中定义的那样@dimen/button_height)

    很棒的 Android 布局问题!

    【讨论】:

    • 确保您了解LinearLayout 的情况。它们是非常有用的布局!如果您在解决问题的同时也从中学到了教训,请继续投票:meta.stackexchange.com/a/198551/187616
    【解决方案2】:

    您的问题与用户体验有关。 您必须决定用户是否愿意滚动到列表末尾以按下添加按钮 或用户想要添加而不滚动到列表末尾。 由于我们的方案只有两种选择, 要么保持添加按钮固定,要么将其添加为列表视图的页脚。

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2020-12-03
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2021-03-14
      相关资源
      最近更新 更多