【问题标题】:Issue while setting Footer layout in ListView在 ListView 中设置页脚布局时出现问题
【发布时间】:2018-05-08 06:52:36
【问题描述】:

下面是我的页脚布局,用于为我的 ListView 设置它:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray_cell_bg">

    <TextView
        android:text="footer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

一切正常。但是,如果我想在我的 ListView 的页脚中设置一个带有某种背景颜色的空视图怎么办?

如果我按照以下方式进行操作,ListView 不会显示页脚视图。 :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray_cell_bg">

    </RelativeLayout>

我将页脚设置如下:

View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_for_gym_list, null);
        listGymSuggestions.addFooterView(footerView);

请指导我如何在 ListView 中设置一些高度和背景颜色的空页脚?

【问题讨论】:

  • 你可以用 View 代替 RelativeLayout ,如果不工作的话,它应该可以工作,让我知道。
  • 您必须提供高度属性。没有任何孩子,您的 Layout 作为页脚将不会显示。
  • 你知道@PragatiSingh 我给出了相同的答案5,但有人一直在投票,甚至没有说。我哪里做错了??
  • 现在他们已经删除了他们的反对票!
  • 除此之外,我还拿了一个 SearchView。并且 onCLose() 方法没有调用..或者请。指南 按下 Android SearchView 的 X(close) 图标时如何触发关闭事件?注意:我知道如果我提出新问题会更好。 ;)

标签: java android listview footer


【解决方案1】:

View 存在,但没有height,因此您必须添加自己的高度值,可能是24dp 或其他内容,并将其设置为页脚,您甚至可以根据需要添加width。例如,在这里我添加了 24dp:

的高度
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="24dp"
    android:background="@color/gray_cell_bg">

</RelativeLayout>

【讨论】:

    【解决方案2】:

    如果该布局中没有子级,则不会显示。如果您在设置特定高度时遇到问题(当您动态添加子项时),您可以使用 padding 之类的,

    android:padding="20dp"
    

    【讨论】:

      【解决方案3】:

      我在其中一个布局中添加了一个按钮作为页脚。 这是我的代码。你可以添加任何你想要的东西。

      <?xml version="1.0" encoding="utf-8"?>
      
      <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <include
          android:id="@+id/search_toolbar"
          layout="@layout/search_toolbar" />
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_horizontal">
      
          <ProgressBar
              android:id="@+id/indeterminateBar"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center" />
      </LinearLayout>
      
      <ListView
          android:id="@+id/listView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_above="@+id/emp_button"
          android:layout_below="@+id/search_toolbar" />
      
      <TextView
          android:id="@+id/tvsample"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/search_toolbar"
          android:layout_marginTop="200dp"
          android:gravity="center_horizontal"
          android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
      
      <android.support.v7.widget.AppCompatImageButton
          android:id="@+id/emp_button"
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:layout_alignParentBottom="true"
          android:background="@color/Red"
          android:src="@drawable/ic_add_employee"
          android:text="@string/emp_button"
          android:textSize="14sp" />
      

      【讨论】:

        【解决方案4】:

        通过在页脚视图中添加 ImageView 解决了问题,如下所示:

        footer.xml

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/gray_cell_bg">
        
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/com_facebook_profilepictureview_preset_size_normal"
                android:scaleType="fitXY"
                android:src="@color/gray_cell_bg" />
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-22
          • 1970-01-01
          • 1970-01-01
          • 2011-11-13
          • 2012-01-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多