如何在android底部导航布局中增加图标大小?

joe*_*oey 14 android bottomnavigationview

我在Android中使用最近由设计库25中的谷歌引入的底部布局导航样式.在我看到的所有教程和问题中,图标中的图像是正常大小,但是我的图像是非常小的,尽管事实是我保存到drawable文件夹的图像是72x72.这是一个截图:

在此输入图像描述

图标应至少为2,甚至可能是该尺寸的3倍.我该怎么做?这是我在bottom_layout.xml中的代码:

  <?xml version="1.0" encoding="utf-8"?>

 <menu xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
    android:id="@+id/menu_home"
    android:title="test"
    android:icon="@drawable/tabbarglossary"
    app:showAsAction="always|withText"
    />
  <item
    android:id="@+id/menu_search"
    android:title="test2"
    android:icon="@drawable/mediationtabbar"
    app:showAsAction="always|withText"
    />

  <item
    android:id="@+id/menu_notifications"
    android:title="test3"
    android:icon="@drawable/ic_action_name"
    app:showAsAction="always|withText"
    />

</menu>
Run Code Online (Sandbox Code Playgroud)

在我的activity_main.xml中:

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:focusable="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    design:menu="@menu/bottom_layout" />
Run Code Online (Sandbox Code Playgroud)

谢谢

bee*_*eeb 29

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

BottomNavigationView bottomNavigationView = (BottomNavigationView) activity.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();
    // set your height here
    layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
    // set your width here
    layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
    iconView.setLayoutParams(layoutParams);
}
Run Code Online (Sandbox Code Playgroud)

编辑

对于图标覆盖文字的问题:

您可以覆盖底部导航视图的某些默认尺寸.例如高度.

<dimen name="design_bottom_navigation_height" tools:override="true">56dp</dimen>
Run Code Online (Sandbox Code Playgroud)

检查指南底部导航以获取默认规格.

  • 这确实可以使图标变大,但现在图标覆盖了我的文本。 (2认同)

Jon*_*tan 15

app:itemIconSize用您的首选值设置属性。


Dan*_*ray 10

将这两行添加到您的底部导航:

app:menu="@menu/main_bottom_navigation"
app:itemIconSize="@dimen/bottom_navigation_icon_size"
Run Code Online (Sandbox Code Playgroud)

另外dimens.xml

<dimen name="bottom_navigation_icon_size" tools:override="true">32dp</dimen>
<dimen name="design_bottom_navigation_height" tools:override="true">72dp</dimen>
Run Code Online (Sandbox Code Playgroud)

要增加图标的大小,请增加bottom_navigation_icon_size。您可能需要更改 的值,design_bottom_navigation_height以便文本不会重叠或出现太多空白。


Bra*_*son 6

我会尝试使用Android的资产工作室,以产生一个通用的图标给你,请确保:

  • 图标的大小为24dp
  • 它有0dp填充

注意:如果您愿意,可以使用自定义图标.

图标生成器

它然后会生成相应的drawable与正确的目录(mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi).

在您的情况下具有静态可绘制尺寸(例如72x72)可能会根据手机的密度更改图标的大小,不同的手机将以不同的方式转换像素.

只需简单地下载zip文件中的图标并将可绘制文件夹解压缩到资源目录,这应该可以解决您的问题.