【问题标题】:Android set menu icon into smallAndroid设置菜单图标变小
【发布时间】:2019-10-08 01:54:07
【问题描述】:

我在这里有一个自定义图标,我希望它变小。

这是我的 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">

    <group android:id="@+id/main_page">
        <item

            android:id="@+id/main_page_search"
            android:state_focused="true"
            android:icon="@drawable/magnify"
            app:showAsAction="always"
            android:title="Search" />
        <item
            android:id="@+id/main_page_check_cart"
            android:icon="@drawable/shopping"
            android:state_focused="true"
            app:showAsAction="always"
            android:title="Check cart" />
    </group>

</menu>

我的 xml 布局视图:

【问题讨论】:

    标签: android xml


    【解决方案1】:

    在下面为每个项目设置例如 25dp;

    android:layout_width 和 android:layout_height

    喜欢

    <item
    
            android:id="@+id/main_page_search"
            android:layout_width = "20dp"
            android:layout_height = "20dp"
            android:state_focused="true"
            android:icon="@drawable/magnify"
            app:showAsAction="always"
            android:title="Search" />
    

    【讨论】:

    • 我尝试了一位先生,没有发现错误但无法正常工作...我不知道我的可绘制尺寸是否是问题...
    【解决方案2】:

    如果你想使用菜单导航,你可以试试这个

    How to change the size of menu item icon in NavigationView?

    或者您可以尝试自定义工具栏,并且您的样式设置为无操作栏

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetStart="16dp"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|center_vertical"
                android:layout_marginEnd="16dp"
                android:background="@drawable/button_menu"
                app:srcCompat="@drawable/ic_search"
                tools:ignore="ContentDescription" />
    
            <ImageView
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_gravity="end|center_vertical"
                android:background="@drawable/button_menu"
                app:srcCompat="@drawable/ic_search"
                tools:ignore="ContentDescription" />
    
        </androidx.appcompat.widget.Toolbar>
    
    </LinearLayout>
    

    没有像这样的操作栏样式

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    【讨论】:

    • 谢谢您,先生,但这仅适用于导航菜单。你可能会检查我附上的图片。这对我的问题不起作用:)
    • 也许你可以试试海关工具栏
    • 我已经尝试过这个并且它正在工作,但是我正在使用导航抽屉先生,所以我无法配置自定义工具栏。
    • 为什么不能配置自定义工具栏?
    • 就像我说的先生,我使用的导航抽屉操作系统无法配置自定义工具栏。虽然如果我是空的活动或基本活动是可能的。
    猜你喜欢
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多