导航抽屉的圆角背景项目

Mil*_*lad 4 android navigation-drawer android-navigationview material-components material-components-android

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

在此处输入图片说明

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

可能吗 ?

Jos*_*ido 6

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

现在,要在XML和Android Studio中为选中的项目添加圆角,颜色,字体和填充,您可以在NavigationView上拥有'app'属性:

<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"/>
Run Code Online (Sandbox Code Playgroud)

使用itemIconTintitemTextColor可以设置是否选中孔项目(图标和文本)的颜色配置。首先,执行res> new>目录,并将目录命名为“ color”。然后,使用新的>颜色资源文件> custom_color_config(名称)在color目录中创建颜色资源文件,并将其放入:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

要添加背景圆角框,请在drawable目录中创建一个新的drawable资源文件以便稍后在itemBackground上进行设置。因此,新建>可绘制资源文件> custom_drawable_resource(名称),并将其放入:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

VOILA!只需将带有一些半粗体字体的自定义样式添加到文本外观即可。

PD:很抱歉,如果我的英语不好,我通常会阅读比写的多的东西,这是MX的问候。

  • “我建议您移至Flutter”。在不了解OP情况的情况下,不应给出这样的现成“建议”。他们想要在布局项目上添加圆角。告诉他们将整个应用程序切换为混乱状态是一个荒谬的建议。 (13认同)

Gab*_*tti 6

只需使用app:itemShapeAppearanceOverlay属性:

<com.google.android.material.navigation.NavigationView
   app:itemShapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Nav"
   ...>
Run Code Online (Sandbox Code Playgroud)

和:

  <style name="ShapeAppearanceOverlay.Nav" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • @Nekomajin42检查/sf/answers/4443686971/ (2认同)

小智 -2

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

图片: