【问题标题】:2 Navigationbars (top and bottom) in android2 android中的导航栏(顶部和底部)
【发布时间】:2019-11-03 19:22:54
【问题描述】:

所以我需要两个导航栏(一个在底部,一个在顶部)。但是正如您在上图中已经看到的那样,由于地图原因,底部的导航栏没有空间(导航栏位于屏幕下方)。如何在不设置地图硬编码宽度和高度的情况下显示底部导航栏。

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main3Activity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/map2"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity"></fragment>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_viedw"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />

</LinearLayout>

【问题讨论】:

    标签: android android-layout navigationbar android-layout-editor


    【解决方案1】:

    当您使用 ConstraintLayout 时,无需使用 LinearLayout,因为 ConstraintLayout 效率更高,并且旨在保持布局层次结构平坦。你可以试试如下图:

    <?xml version="1.0" encoding="utf-8"?>
    <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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Main3Activity">
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:menu="@menu/bottom_nav_menu" />
    
        <fragment
            android:id="@+id/map2"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/nav_viedw"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/nav_info"
            tools:context=".MapsActivity" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_viedw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/nav_info"
            app:menu="@menu/bottom_nav_menu" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      在您想要显示底部导航的片段中提供一些底部边距,因为现在您的片段占据了顶部导航下方的所有布局高度。

      【讨论】:

        【解决方案3】:
        <?xml version="1.0" encoding="utf-8"?>
        <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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
        <fragment
            android:id="@+id/map2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            tools:context=".MapsActivity"
            app:layout_constraintTop_toBottomOf="@+id/nav_info"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/nav_viedw"/>
        
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_viedw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 2020-07-08
          • 1970-01-01
          • 2017-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-16
          相关资源
          最近更新 更多