【问题标题】:BottomNavigationView height is not wrapped to the bottomBottomNavigationView 高度未包裹到底部
【发布时间】:2020-03-10 00:19:24
【问题描述】:

我有一个带有底部导航视图的线性布局,问题是它没有高度,我不知道为什么

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ToolbarTheme" />

    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemIconTint="@drawable/bottom_nav_color"
            app:itemTextColor="@drawable/bottom_nav_color"
            app:menu="@menu/bottom_nav_menu" />

</LinearLayout>

输出

例如,如果我将 100dp 作为高度,它会从底部扩展,但我仍然看不到它

所以问题是,如果我将底视图放在线性布局的末尾,我看不到底视图,知道为什么吗?

【问题讨论】:

    标签: java android kotlin bottomnavigationview android-bottomnavigationview


    【解决方案1】:

    通过添加相对布局解决了它

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mainrlot"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/colorPrimary"
                    android:theme="@style/ToolbarTheme" />
    
            </com.google.android.material.appbar.AppBarLayout>
    
            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/mobile_navigation" />
    
        </LinearLayout>
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            app:itemIconTint="@drawable/bottom_nav_color"
            app:itemTextColor="@drawable/bottom_nav_color"
            app:menu="@menu/bottom_nav_menu" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      要设置布局在底部的位置,请添加此代码。 android:layout_gravity="bottom"

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <com.google.android.material.bottomnavigation.BottomNavigationView
                  android:id="@+id/nav_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android: layout_gravity="bottom"
                  app:itemIconTint="@drawable/bottom_nav_color"
                  app:itemTextColor="@drawable/bottom_nav_color"
                  app:menu="@menu/bottom_nav_menu" />
      
      </LinearLayout>
      

      【讨论】:

      • 在提问之前尝试过,但这并没有解决我的问题,谢谢
      猜你喜欢
      • 2015-10-31
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多