【问题标题】:Adding a Button at the end of ListView在 ListView 的末尾添加一个 Button
【发布时间】:2015-09-16 19:33:40
【问题描述】:

我想在列表视图的末尾添加一个按钮。我有以下 xml 文件。列表视图及其内容出现,但按钮不显示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

【问题讨论】:

  • 学习RelativeLayout

标签: android xml android-layout listview android-button


【解决方案1】:

问题在于 ListView 中的属性 android:layout_height="match_parent"match_parent 表示列表视图将填满所有空间。正确的做法是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weight="1"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

【讨论】:

    【解决方案2】:

    您的按钮未显示,因为当您将其 layout_height 设置为 ma​​tch_parent 时,ListView 占据了隐藏按钮的整个空间。 match_parent 将占用整个可用空间。因此,将其更改为 wrap_content,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_parent"
            android:id="@+id/myListView" />
        <Button
            android:text="Project Download"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/download" />
    </LinearLayout>
    

    wrap_content 会将您的视图包装到可用的最小高度/宽度,而不会隐藏任何内容或使任何内容不清楚。

    【讨论】:

      【解决方案3】:

      我有同样的问题,并解决了。 您只需将该布局包装到 ScrollView 中

      <ScrollView 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" >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
      
                  <android.support.v7.widget.RecyclerView
                      android:id="@+id/rv_transaction_history"
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="1">
      
                  </android.support.v7.widget.RecyclerView>
      
                  <Button
                      android:id="@+id/btn_history_load_more"
                      style="?borderlessButtonStyle"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:fontFamily="sans-serif-medium"
                      android:paddingBottom="@dimen/margin_big"
                      android:paddingTop="@dimen/margin_large"
                      android:text="@string/text_btn_load_more"
                      android:textColor="#009CDE"
                      android:textSize="@dimen/text_size_medium" />
          </LinearLayout>
      </ScrollView>
      

      【讨论】:

        【解决方案4】:
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        
        <Button
            android:layout_alignParentBottom="true"
            android:text="Project Download"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/download" />
        
        <ListView
            android:layout_above="@id/download"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/myListView" /></RelativeLayout>
        

        【讨论】:

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