【问题标题】:Navigation drawer rounded corner background for items项目的导航抽屉圆角背景
【发布时间】:2018-10-29 22:01:21
【问题描述】:

我想为导航抽屉中的项目设置一个圆角,如下所示:

这是material.io网站中的材料设计示例

有可能吗?

【问题讨论】:

    标签: android navigation-drawer android-navigationview material-components material-components-android


    【解决方案1】:

    只需使用 app:itemShapeAppearanceOverlay 属性:

    <com.google.android.material.navigation.NavigationView
       app:itemShapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Nav"
       ...>
    

    与:

      <style name="ShapeAppearanceOverlay.Nav" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">8dp</item>
      </style>
    

    【讨论】:

    【解决方案2】:

    首先,我建议您迁移到 Flutter,它更直观,并且您获得了 Material 指南的最佳集成流程。

    现在,要使用 XML 和 Android Studio 为选中的项目添加圆角、颜色、字体和填充,您可以在 NavigationView 上使用“应用”属性:

    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        ...>
    
    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        ...
        app:itemIconTint="@color/custom_color_config"
        app:itemTextColor="@color/custom_color_config"
        app:itemBackground="@drawable/custom_drawable_resource"
        app:itemTextAppearance="@style/custom_style"/>
    

    使用 itemIconTintitemTextColor,您可以在选中或未选中时设置孔项目(图标和文本)的颜色配置。首先,执行 res > new > directory,并将目录命名为“color”。然后,在 color 目录中创建 颜色资源文件,使用 new > color resource file > custom_color_config (name) 并输入:

    <?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:color="your_checked_item_color"
            android:state_checked="true" />
        <item
            android:color="your_non_checked_item_color"/>
    </selector>
    

    具有 state_checked=true 属性的项目会将其颜色应用于当前导航选中的项目。

    要添加背景圆角框,请在 drawable 目录中创建一个新的 drawable 资源文件,以便稍后在 itemBackground 中设置。所以,new > drawable resource file > custom_drawable_resource(name) 并把这个:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:left="9dp"
            android:right="9dp"
            android:top="2dp"
            android:bottom="2dp">
    
            <shape>
                <solid android:color="@color/new_color_resource_name"/>
                <corners android:radius="5dp"/>
            </shape>
    
        </item>
    </layer-list>
    

    接下来,在 color 目录中再次创建第二个 颜色资源文件,以与文件 custom_drawable_resource (new_color_resource_name) 中的纯色属性相关联,并在其中放置: p>

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item 
            android:color="your_background_checked_item_color"
            android:state_checked="true" />
        <item 
            android:color="your_background_non_checked_item_color"/>
    </selector>
    

    并且 VOILA! 只需在文本外观中添加带有一些半粗体字体的自定义样式。

    PD:对不起,如果我的英语不好,我通常读的比写的多,来自 MX 的问候。

    【讨论】:

    • "我建议你转向 Flutter" 在不了解 OP 的情况的情况下,你不应该像这样即兴地给出“建议”。他们想要布局项目上的圆角。告诉他们将整个应用程序切换为 Flutter 是一个荒谬的建议。
    【解决方案3】:

    这是通过 navigationview 和 menuItem 完成的,并为圆形边框创建一个 shape 文件,并在 selected_checked 处于活动状态和停用时创建 selected_state 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2023-03-08
      • 2019-11-25
      相关资源
      最近更新 更多