【问题标题】:BottomNavigationView is not full widthBottomNavigationView 不是全宽
【发布时间】:2017-05-16 22:03:28
【问题描述】:

我正在开发一个 Android 应用程序并从设计库中实现 BottomNavigationView。我查看了许多示例,但无法弄清楚我的布局有什么问题。 BottomNavigationView 未显示为全宽。

另一个问题是没有应用状态栏颜色。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Include the toolbar -->
<include layout="@layout/toolbar"/>

<RelativeLayout android:id="@+id/fragment_container"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/bottom_navigation_main"/>

</android.support.design.widget.CoordinatorLayout>

工具栏.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

编辑: 增加了未设置状态颜色的解决方案

android:fitsSystemWindows="true"

(colorPrimaryDark) status bar color not working on android v21?

【问题讨论】:

    标签: android material-design bottom-sheet


    【解决方案1】:

    BottomNavigationView 不显示为全宽。

    这是不应该的。

    根据design guidelines,动作的宽度可以在 80dp 和 168dp 之间变化。您定义的两个动作不足以水平填充整个区域。
    (附带说明一下,根据指南,视图应该包含三到五个动作。)

    如果要填充BottomNavigationView后面的空间,可以将view的背景颜色设置为与items背景相同的颜色:

    <android.support.design.widget.BottomNavigationView
         android:background="@color/bottom_view_color"
         app:itemBackground="@color/bottom_view_color"
    
         // .... />
    

    【讨论】:

    • 底部导航在 Google Play 报亭中似乎是全宽的,所以我认为我的布局有问题。我计划很快添加第三个菜单项。
    • @midhunhk 是的,这是可能的。我为此添加了解决方案。但是安迪是正确的,你应该遵循设计规范。
    【解决方案2】:

    这是可行的。但它会反对default design specs,我建议你选择default design specs


    现在来到我的解决方案...

    将以下维度定义为dimens.xml。这些尺寸应为valuesvalues-largevalues-large-land。并且600dp 可以在values-largevalues-large-land 中增加到1000dp 或更多,如果在平板电脑中您看不到这种变化。

    <resources xmlns:tools="http://schemas.android.com/tools">
        <dimen name="design_bottom_navigation_item_max_width" tools:override="true">600dp</dimen>
        <dimen name="design_bottom_navigation_active_item_max_width" tools:override="true">600dp</dimen>
    </resources>
    

    就是这样!!!结果会像


    这不是魔术。

    为什么添加了具有这样名称和值的维度是 600dp

    BottomNavigationMenuView.javaBottomNavigationView 中用于表示菜单的类)使用了这两个维度。 下面是代码

    public BottomNavigationMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        ...
        mInactiveItemMaxWidth = res.getDimensionPixelSize(
                R.dimen.design_bottom_navigation_item_max_width);
        ....
        mActiveItemMaxWidth = res.getDimensionPixelSize(
                R.dimen.design_bottom_navigation_active_item_max_width);
        .....
    }
    

    现在这些值用于创建固定宽度的视图,如下所示

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ....
        if (mShiftingMode) {
            final int inactiveCount = count - 1;
            final int activeMaxAvailable = width - inactiveCount * mInactiveItemMinWidth;
            final int activeWidth = Math.min(activeMaxAvailable, mActiveItemMaxWidth);
            final int inactiveMaxAvailable = (width - activeWidth) / inactiveCount;
            final int inactiveWidth = Math.min(inactiveMaxAvailable, mInactiveItemMaxWidth);
            ...
        } else {
            final int maxAvailable = width / count;
            final int childWidth = Math.min(maxAvailable, mActiveItemMaxWidth);
            .....
        }
        ....
    }
    

    为了始终使用activeMaxAvailable 的值,我将一个虚拟值设置为mActiveItemMaxWidth(以上面的尺寸表示)。所以activeWidth会有 activeMaxAvailable 的值。同样的规则适用于inactiveWidth

    所以当你构建项目时,design_bottom_navigation_item_max_widthdesign_bottom_navigation_active_item_max_width 定义了 design-support-lib,将被我们定义的尺寸替换。

    代码也在最大支持选项(最多 5 个)上验证。

    【讨论】:

    • 我正面临移动底部导航显示完美的问题,但是当我尝试在平板电脑中底部标签显示在中心时出现什么问题?
    • 您还需要为平板电脑声明尺寸。该值必须大于手机的值
    • 好的,但我使用 SDP 库进行通用设计,它根据屏幕尺寸处理仍然面临同样的问题。
    • @Arbaz.in 您为平板电脑增加了什么价值?
    • 完美!!!有用!正是我一直在寻找的!谢谢,很有帮助!=)
    【解决方案3】:

    我建议只使用

    android:background="@color/bottom_view_color"
    

    它会将条形显示为全宽,并在用户单击项目时保持涟漪效果。

    如果你还添加app:itemBackground,你将失去涟漪效应。

    你的情况

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/bottom_navigation_main"/>
    

    【讨论】:

      【解决方案4】:

      这是我的解决方案:

      app:itemHorizontalTranslationEnabled = "false"
      

      默认为真。这会“扩展”项目以适应整个宽度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 2015-12-20
        • 2022-01-24
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 2021-12-05
        相关资源
        最近更新 更多