更改BottomNavigationView的图标大小

时间:2017-01-07 00:38:42

标签: android bottomnavigationview

我一直在尝试使用新的BottomNavigationView并尝试自定义它。

到目前为止,我已设法使用以下内容更改高度和边距:

<dimen name="design_bottom_navigation_height" tools:override="true">75dp</dimen>
<dimen name="design_bottom_navigation_margin" tools:override="true">5dp</dimen>

我想增加图标的大小。

如何做到这一点?

编译版本:com.android.support:design:25.0.1

5 个答案:

答案 0 :(得分:29)

图标大小在项目布局中硬编码为24dp(请参阅design_bottom_navigation_item.xml),并且可以通过编程方式进行更改:

BottomNavigationView bottomNavigationView = (BottomNavigationView) configurationActivity.findViewById(R.id.bottom_navigation_view);
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
for (int i = 0; i < menuView.getChildCount(); i++) {
    final View iconView = menuView.getChildAt(i).findViewById(android.support.design.R.id.icon);
    final ViewGroup.LayoutParams layoutParams = iconView.getLayoutParams();
    final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
    layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
    iconView.setLayoutParams(layoutParams);
}

答案 1 :(得分:16)

最新但最新

使用implementation 'com.android.support:design:28.0.0'设计支持库。

有一个属性可以更改图标大小:

<android.support.design.widget.BottomNavigationView
    app:itemIconSize="@dimen/_26sdp"
    ....
    ....
</android.support.design.widget.BottomNavigationView>

以编程方式:

dashboardNavigation.setItemIconSize(24);

谢谢。

答案 2 :(得分:1)

这与AndroidX完美搭配 您的XML布局

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    style="@style/DA_Bottom_Nav"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    app:itemIconSize="50dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/nav_menu"/>

Res / values / dimens.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <dimen name="design_bottom_navigation_height" tools:override="true">80dp</dimen>
</resources>

答案 3 :(得分:0)

对于 androidx ,请将此ID用于图标 andcom.google.android.material.R.id.icon

完整代码:

BottomNavigationView bottomNavigationView = (BottomNavigationView) configurationActivity.findViewById(R.id.bottom_navigation_view);
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
for (int i = 0; i < menuView.getChildCount(); i++) {
    final View iconView = menuView.getChildAt(i).findViewById(com.google.android.material.R.id.icon);
    final ViewGroup.LayoutParams layoutParams = iconView.getLayoutParams();
    final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
    layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
    iconView.setLayoutParams(layoutParams);
}

答案 4 :(得分:0)

如果您想更改 BottomNavigationView 图标的大小,这里有一个快速简便的解决方案。

假设您的 BottomNavigationView 上有 5 个项目并且您想要更改中间图标大小:

BottomNavigationMenuView menuView = (BottomNavigationMenuView)
            mainBinding.bottomNavView.getChildAt(0);
// Here the index: 2 at 'getChildAt(2)' means the middle icon
BottomNavigationItemView navigationItemView = (BottomNavigationItemView) menuView.getChildAt(2);

final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
            
navigationItemView.setIconSize((int)
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40,
                    displayMetrics));

现在你已经改变了;)