如何更改安卓顶部工具栏菜单项图标大小

11

我该如何在 Android 工具栏中更改菜单项的大小?目前菜单项非常小,我想增加它们的大小。如果有人知道,请帮助我找到解决方案。

app_bar.xml

  <?xml version="1.0" encoding="utf-8"?>
   <android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/primaryColor"
   android:minHeight="?attr/actionBarSize"
   android:paddingTop="@dimen/app_bar_top_padding"
   android:theme="@style/ToolBarStyle"/>

样式表.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="AppTheme.Base"> </style>
<!-- Application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item>
    <item name="colorControlHighlight">@color/primary_high_light</item>
    <!-- <item name="textColorPrimary">@color/ColorPrimaryText</item> -->
</style>

<style name="ToolBarStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textColorPrimary">#ffffff</item>

</style>
<style name="option" parent="Theme.AppCompat.Light">
</style>
<style name="Dialog" parent="Base.Theme.AppCompat.Light.Dialog">
</style>

菜单选项.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/homes"
    android:title="homes"
    android:icon="@drawable/ic_action_home1"
    app:showAsAction="always"/>

<item android:id="@+id/profilepreview"
    android:title="ProfilePreview"
    android:icon="@drawable/profile"
    app:showAsAction="always"/>

<item android:id="@+id/findPeople"
    android:title="findPeople"
    android:icon="@drawable/ic_action_search1"
    app:showAsAction="always"/>

<item android:id="@+id/log"
    android:title="log"
    android:icon="@drawable/ic_action_logout1"
    app:showAsAction="always"/>

</menu>

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_option, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

            case R.id.profilepreview:
            startActivity(new Intent(this, ProfilePreview.class));
            return true;

            case R.id.homes:
            mFragment = new HomeFragment();
            break;

            case R.id.findPeople:
            mFragment = new FindFriendsFragment();
            break;

            case R.id.log:
            M.logOut(this);
            mIntent = new Intent(this, LoginActivity.class);
            finish();

    }

    if (mFragment != null && mIntent == null) {
        mFragmentManager = getFragmentManager();
        mFragmentManager.beginTransaction()
                .replace(R.id.frameContainer, mFragment).commit();
    } else {
        startActivity(mIntent);
        mIntent = null;
    }return super.onOptionsItemSelected(item);
    }

http://www.android4devs.com/2014/12/how-to-make-material-design-app.html - arodriguezdonaire
我确实尝试了那个教程,但是没有任何选项可以更改菜单图标的大小。 - Prins Prem
1
@PrinsPrem 我也遇到了工具栏内图标大小的问题。想知道你是否找到了解决方法?提前感谢。 - Amin
有什么解决方案吗?我也遇到了这个问题。 - Lion789
这可以在 onCreateOptionsMenu 中设置。请参考我的答案这里 - 6rchid
4个回答

8
您可以设计自定义布局并将其添加为您的actionlayout。还要确保不在menu.xml文件中添加任何图标。
<item android:id="@+id/homes"
android:title="homes"
app:actionLayout="@layout/your_custom_layout"
app:showAsAction="always"/>

然后在您的自定义布局中给出min_height。

1

我也遇到了这个问题,唯一的解决办法就是使用更小的图片。从这里的图标中选择24dp的尺寸(https://design.google.com/icons),可以很好地适应我的工具栏。


1
我建议您阅读此博客以了解自定义工具栏。

https://stablekernel.com/using-custom-views-as-menu-items/

不必将drawable_icon添加到您的menu-xml文件中,您可以在该menu-xml文件中使用"app:actionLayout"来指定其他xml文件(您将在其中创建自定义工具栏图标)。


0

只需将矢量图像(在右侧)的大小从默认的24x24增加到不同的大小,但是在某个大小之后它将无法缩放。如果将其放置在自定义工具栏中与充气视图(左侧橙色背景)并排显示,则大小可能看起来很奇怪,唯一解决方法是在工具栏中充气另一个视图,并将矢量作为背景。enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接