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

问题描述 投票:0回答:4

如何更改 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>

menu_option.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);
    }
android android-toolbar
4个回答
8
投票

您可以设计自定义布局并将其添加为您的操作布局。还要确保您没有在 menu.xml 文件中添加任何图标。

<item android:id="@+id/homes"
android:title="homes"
app:actionLayout="@layout/your_custom_layout"
app:showAsAction="always"/>

然后在您的自定义布局中提供 min_height。


1
投票

我也遇到了这个问题,唯一的解决方法是使用较小的图像。此处图标 (https://design.google.com/icons) 的 24dp 大小非常适合我的工具栏。


1
投票

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

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

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


0
投票

只需将矢量图像(右侧)的大小从默认的 24x24 增加到不同的大小,但在一定大小后它不会按比例放大。如图所示,如果将它放在自定义工具栏中膨胀的视图(左侧为橙色背景)旁边,大小可能看起来很奇怪,解决此问题的唯一方法是使用矢量作为背景膨胀工具栏中的另一个视图.

© www.soinside.com 2019 - 2024. All rights reserved.