【问题标题】:Add a button in bottom of listview when the listview is empty (using ListActivity)当列表视图为空时,在列表视图底部添加一个按钮(使用 ListActivity)
【发布时间】:2013-05-23 18:10:17
【问题描述】:

当列表为空时,我使用扩展 ListActivity 来显示空消息。 下面是我的xml文件:

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

    <ListView android:id="@+id/android:list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1.0">
    </ListView>

    <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No Items! Please Add!"/> 

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"        
        android:text="Add Item" />

</LinearLayout>

我的问题是按钮只能在列表中有项目时出现,但在列表为空时不能出现。列表为空时,按钮怎么出现?

【问题讨论】:

标签: android listview layout


【解决方案1】:

首先检查getCount()。如果getCount()返回0,那么按钮的visibility应该可见,否则'invisible''gone'

【讨论】:

    【解决方案2】:

    只需使用xml 中的android tag android:visible buttons 代码即可设置参考ID

    button.setVisibility(View.Visible or View.inVisible) based on your requirement.
    

    【讨论】:

      【解决方案3】:

      您应该在列表视图的布局中创建一个页脚视图布局。

      View footer = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
      
      ListView.addFooterView(footer);
      

      同时检查this link

      【讨论】:

        【解决方案4】:

        您可以通过执行检查 ListView 中是否没有项目

        listview.getAdapter().isEmpty
        

        如果是,请将按钮的可见性设置为gone。否则,visible

        【讨论】:

          【解决方案5】:

          好吧,我不是要添加页脚,而只需要添加一个按钮。我如下更改xml然后问题解决。谢谢大家。^^

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" 
                 >
          
              <ListView android:id="@+id/android:list"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:layout_above="@+id/bAdd">
              </ListView>
          
          
              <TextView android:id="@android:id/empty"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:layout_above="@+id/bAdd"
                         android:text="No Items! Please Add!"/> 
          
               <Button
                  android:id="@+id/bAdd"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_alignParentRight="true"        
                  android:text="Add Item" />
          
          
          
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多