【问题标题】:How to make ListView resizable?如何使 ListView 可调整大小?
【发布时间】:2020-02-13 09:57:20
【问题描述】:

图片1

图2

如何使 ListView 可调整大小?

这个ListView没问题。 OK ListView 1 图片

OK ListView 2 图片

我已经尝试了所有的布局。这没有帮助。

我有一个片段容器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragmentsContainer"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:menu="@menu/menu"
        android:id="@+id/bottomNavigation"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

OK 布局是搜索布局:

<?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:background="@color/yellow"
    android:id="@+id/searchAndListViewContainer"
    android:orientation="vertical">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorOfSearchView" />

    <ListView
        android:id="@+id/downloadedMusicListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

无法适应的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/darkGreen">

    <ListView
        android:id="@+id/musicListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

如您所见,Java 代码将更改 Fragments 容器的内部内容。 两个片段几乎相同。 有什么问题?

【问题讨论】:

  • layout_weight="1" // 是问题所在。
  • 只制作页眉 - 框架布局 - 线性布局内的页脚,看看区别
  • @Radhey 删除 weight = 1 没有帮助。

标签: java android listview


【解决方案1】:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tv_footer"
            android:layout_below="@+id/tv_header" />

        <TextView
            android:id="@+id/tv_footer"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="#ff0000"
            android:gravity="center"
            android:text="Footer" />

        <TextView
            android:id="@+id/tv_header"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="#00ff00"
            android:gravity="center"
            android:orientation="vertical"
            android:text="Header" />        

   </RelativeLayout>

您可以通过多种方式做到这一点。在这里您可以将自己的视图设置为页眉和页脚。

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 2015-01-19
    • 2011-12-28
    • 2010-09-28
    • 2015-02-07
    • 2023-03-25
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多