You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Java中自定义ListView搭配底部导航栏的实现及显示异常解决

解决Android中ListView与BottomNavigationView共存显示问题

你的布局存在几个关键问题,导致BottomNavigationView被ListView完全遮挡:

  • RelativeLayout不支持android:orientation="vertical"属性,属于无效配置
  • RelativeLayout的android:layout_height="wrap_content"会让父布局无法撑满屏幕,配合ListView的match_parent高度,会让ListView占据所有可用空间
  • ListView设置的android:layout_alignParentBottom="true"加上match_parent高度,即便留了layout_marginBottom,也会因为RelativeLayout的层级渲染逻辑导致底部导航被覆盖

以下是几种可行的修复方案:

方案一:修复原RelativeLayout布局

修改父布局高度为match_parent,移除无效属性,同时让ListView通过layout_above约束在底部导航上方:

<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:theme="@style/Theme.AppTheme"
    app:titleTextAppearance="@font/montserrat_regular_400"
    tools:context=".AddedDevicesActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomNavigationBar"
        tools:listitem="@layout/list_item"/>

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

</RelativeLayout>

方案二:改用LinearLayout(简单直观)

用垂直方向的LinearLayout,通过layout_weight让ListView占据除底部导航外的所有空间:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@style/Theme.AppTheme"
    app:titleTextAppearance="@font/montserrat_regular_400"
    tools:context=".AddedDevicesActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:listitem="@layout/list_item"/>

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

</LinearLayout>

方案三:使用ConstraintLayout(现代Android布局首选)

通过约束关系明确ListView和底部导航的位置,适配性更强:

<androidx.constraintlayout.widget.ConstraintLayout 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:theme="@style/Theme.AppTheme"
    app:titleTextAppearance="@font/montserrat_regular_400"
    tools:context=".AddedDevicesActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationBar"
        tools:listitem="@layout/list_item"/>

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

</androidx.constraintlayout.widget.ConstraintLayout>

补充:底部导航栏点击事件处理

在Activity中添加导航点击逻辑示例:

BottomNavigationView bottomNav = findViewById(R.id.bottomNavigationBar);
bottomNav.setOnItemSelectedListener(item -> {
    switch (item.getItemId()) {
        case R.id.menu_item1:
            // 对应菜单项的处理逻辑
            return true;
        case R.id.menu_item2:
            // 对应菜单项的处理逻辑
            return true;
        default:
            return false;
    }
});

内容的提问来源于stack exchange,提问作者Abhay Jadon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:15:40