更改 BottomNavigationView 的图标大小
Changing BottomNavigationView's Icon Size
我一直在试验新的 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
图标大小在项目布局中硬编码为 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);
}
晚但最新
使用 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);
更新:
如果您使用的是 material 库,那么它将是相同的。只需更改包名称如下。
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" />
以编程方式 - 同上。
谢谢。
对于 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);
}
这对我来说非常适合 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>
如果您想更改 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));
现在你已经改变了 ;)
我一直在试验新的 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
图标大小在项目布局中硬编码为 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);
}
晚但最新
使用 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);
更新:
如果您使用的是 material 库,那么它将是相同的。只需更改包名称如下。
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" />
以编程方式 - 同上。
谢谢。
对于 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);
}
这对我来说非常适合 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>
如果您想更改 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));
现在你已经改变了 ;)