【发布时间】:2018-10-29 22:01:21
【问题描述】:
【问题讨论】:
标签: android navigation-drawer android-navigationview material-components material-components-android
【问题讨论】:
标签: android navigation-drawer android-navigationview material-components material-components-android
只需使用 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>
【讨论】:
首先,我建议您迁移到 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"/>
使用 itemIconTint 和 itemTextColor,您可以在选中或未选中时设置孔项目(图标和文本)的颜色配置。首先,执行 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 的问候。
【讨论】: