【问题标题】:Changing BottomNavigationView's Icon Size更改 BottomNavigationView 的图标大小
【发布时间】:2017-01-07 00:38:41
【问题描述】:

我一直在试验新的 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

【问题讨论】:

标签: android bottomnavigationview


【解决方案1】:

迟到但最新

使用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);

更新:

如果您使用的是材质库,那么它是相同的。只需将包名称更改如下。

implementation 'com.google.android.material:material:1.1.0'

XML:

<com.google.android.material.bottomnavigation.BottomNavigationView
                app:itemIconSize="@dimen/_26sdp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

以编程方式 - 同上。

谢谢。

【讨论】:

  • 是否可以通过编程方式更改它?
  • 如何在BottomNavigationView中改变文字大小
  • 太棒了。谢谢
【解决方案2】:

图标大小在项目布局中被硬编码为 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);
}

【讨论】:

  • 我不敢相信他们硬编码大小并且没有使用样式属性来设置可以为开发人员带来更多灵活性的值...
  • 我爱你兄弟,你拯救了我的一天!
  • 如何在 kotlin 上获取 bottomNavigationView,因为我得到了 NPE:val bottomNavigationView = bottom_navigation_view
  • 为什么我们需要调用 bottomNavigationView.getChildAt(0) ?或者 BottomNavigationView 和 BottomNavigationMenuView 有什么区别?
  • androidx 包的资源名称是什么
【解决方案3】:

对于 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);
}

【讨论】:

  • 由于 material:1.4.0 现在使用的 id 是:com.google.android.material.R.id.navigation_bar_item_icon_view
【解决方案4】:

这对我来说非常适合 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>

【讨论】:

    【解决方案5】:

    如果您想更改 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));
    

    现在你已经改变了;)

    【讨论】:

    • 看起来很老套,但不是最好的主意,也不是可扩展的。当你这样做时:“NavigationBarItemView.setIconSize 只能从同一个库组中调用 (groupId=com.google.android.material)”
    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2020-01-08
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多