【问题标题】:Changing NavigationView background to round shape not working when pressed按下时将 NavigationView 背景更改为圆形不起作用
【发布时间】:2018-12-01 20:04:06
【问题描述】:

我在布局中使用app:itemBackground 设置导航项背景:

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:itemBackground="@drawable/nav_selector_background"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

这里是nav_selector_background.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorNavItemSelected">
<item android:id="@android:id/mask">
    <color android:color="#000"/> <!-- any color will do -->
</item>

<item
        android:right="8dp">
    <selector>
        <item android:state_pressed="true" android:drawable="@drawable/nav_item_background_round"/>
        <item android:state_focused="true" android:drawable="@drawable/nav_item_background_round"/>
        <item android:state_checked="true" android:drawable="@drawable/nav_item_background_round"/>
        <item android:state_activated="true" android:drawable="@drawable/nav_item_background_round"/>
        <item android:state_active="true" android:drawable="@drawable/nav_item_background_round" />
        <item android:state_hovered="true" android:drawable="@drawable/nav_item_background_round" />
        <item android:state_drag_hovered="true" android:drawable="@drawable/nav_item_background_round" />
        <item android:drawable="@android:color/transparent"/>
    </selector>

</item>

还有nav_item_background_round

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/colorNavItemSelected"/>
            <corners android:bottomRightRadius="32dp"
                     android:topRightRadius="32dp"/>
        </shape>
    </item>
</layer-list>

问题是当我按下项目时,整个背景都会突出显示,如屏幕截图所示。我只想突出显示红色部分。

如果有人对编译和运行应用程序感兴趣,我将示例上传到 Github

【问题讨论】:

    标签: android android-layout android-navigationview material-components material-components-android


    【解决方案1】:

    MaterialComponents libarry 中使用NavigationView,您可以使用:

    • app:itemShapeAppearanceOverlay 属性为每个项目定义自定义形状
    • app:itemShapeFillColor 属性来定义用于填充形状的颜色

    类似:

      <com.google.android.material.navigation.NavigationView
          app:itemShapeFillColor="@color/selector_menu"
          app:itemShapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Nav"
          android:theme="@style/ThemeOverlay.NavigationView"
          ../>
    

    选择器类似于:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:alpha="0.50" android:color="@color/primaryColor" android:state_pressed="true"/>
      <item android:alpha="0.12" android:color="@color/primaryColor" android:state_activated="true"/>
      <item android:alpha="0.12" android:color="@color/primaryColor" android:state_checked="true"/>
      <item android:color="@android:color/transparent"/>
    </selector>
    

    形状定义为:

      <style name="ShapeAppearanceOverlay.Nav" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSizeTopRight">50%</item>
        <item name="cornerSizeBottomRight">50%</item>
        <item name="cornerSizeBottomLeft">0dp</item>
        <item name="cornerSizeTopLeft">0dp</item>
      </style>
    

    还可以使用android:theme 覆盖波纹使用的颜色。

      <style name="ThemeOverlay.NavigationView" parent="">
        <item name="android:colorControlHighlight">@android:color/transparent</item>
      </style>
    

    【讨论】:

    • 我正在尝试使用app:itemShapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Nav",但出现以下错误style/ShapeAppearanceOverlay.Nav) not found 我正在使用material:1.1.0-beta01 任何想法
    • @NileshRathod 对不起我的错。我刚刚更新了答案。
    • 很好的解决方案。但导航项右侧没有空格。
    【解决方案2】:

    我不确定你想要什么,但我认为这会解决你的问题。

    nav_item_background_round.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:color="#f816a463"
            tools:targetApi="lollipop">
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="#f816a463" />
            </shape>
        </item>
    </ripple>
    

    【讨论】:

    • 它不起作用。我想要的是在一侧有圆形末端,但如图所示,按下时突出显示的背景与圆形末端重叠。我想要与 Android 版 Google Keep 应用中相同的效果。
    【解决方案3】:

    我也有同样的问题。我解决了它

    首先 - 将自定义样式添加到我的 navigationView app:theme="@style/NavigationDrawerStyle"

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fadingEdge="none"
        android:fitsSystemWindows="true"
        android:overScrollMode="never"
        app:itemIconPadding="12dp"
        app:itemBackground="@drawable/drawer_background_selector"
        app:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@drawable/drawer_item_icon_color"
        app:itemTextColor="@drawable/drawer_item_text_color"
        app:theme="@style/NavigationDrawerStyle"
        app:menu="@menu/activity_main_drawer" >
    

    第二 - 然后在我的 styles.xml 中创建了一个自定义抽屉样式 NavigationDrawerStyle 然后 将 colorControlHighlight 设置为透明,这为我移除了矩形。

    <style name="NavigationDrawerStyle" parent="ThemeOverlay.AppCompat.navTheme">
        <item name="android:textSize">14sp</item>
        <item name="android:fontFamily">@font/robotomedium</item>
        <item name="android:colorControlHighlight">@android:color/transparent</item><!--Here Removes Rectangle-->
    </style>
    

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 1970-01-01
      • 2014-11-05
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2022-11-21
      • 1970-01-01
      相关资源
      最近更新 更多