Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-更改NavigationView中单个项目的背景色_Android_Kotlin - Fatal编程技术网

Android-更改NavigationView中单个项目的背景色

Android-更改NavigationView中单个项目的背景色,android,kotlin,Android,Kotlin,我的应用程序中有标准的安卓、材质、导航视图,我想知道是否可以更改列表中第一个元素的背景色 我试图通过获取标题字符串并设置BackgroundColorSpan来实现这一点,但它只会更改TextView的背景色 val proItem = nav_view.menu.findItem(R.id.pro_item) val spanString = SpannableString(proItem.title) spanString.setSpan(Background

我的应用程序中有标准的安卓、材质、导航视图,我想知道是否可以更改列表中第一个元素的背景色

我试图通过获取标题字符串并设置
BackgroundColorSpan
来实现这一点,但它只会更改TextView的背景色

val proItem = nav_view.menu.findItem(R.id.pro_item)
        val spanString = SpannableString(proItem.title)
        spanString.setSpan(BackgroundColorSpan(R.color.colorAccent), 0, spanString.length, 0)
        proItem.title = spanString
我一直在寻找类似于NavigationView中的
app:itemBackground
标记的东西,但它对列表中的每一项都会这样做,而我只想在其中一项上这样做


有什么方法可以实现吗?

应用程序:itemBackground
可用于更改背景颜色。您可以为选中状态创建选择器,并在xml中引用该选择器

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- focused -->
    <item android:state_checked="true" android:drawable="@color/btn_color_green_alpha"></item>
</selector>

colors.xml

<color name="btn_color_green_alpha">#3300ff00</color>
#3300ff00
布局文件

<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/selector"   //line to be added
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main2_drawer" />

希望这有帮助

如果您只需要一个选定颜色的项目,并且不想应用于其他项目,则可以将其他项目设置为可检查=false

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="Import" />
    <item
        android:id="@+id/nav_gallery"
        android:checkable="false"                  //line added here
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />
    </group>
</menu>

应用程序:itemBackground
可用于更改背景颜色。您可以为选中状态创建选择器,并在xml中引用该选择器

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- focused -->
    <item android:state_checked="true" android:drawable="@color/btn_color_green_alpha"></item>
</selector>

colors.xml

<color name="btn_color_green_alpha">#3300ff00</color>
#3300ff00
布局文件

<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/selector"   //line to be added
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main2_drawer" />

希望这有帮助

如果您只需要一个选定颜色的项目,并且不想应用于其他项目,则可以将其他项目设置为可检查=false

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="Import" />
    <item
        android:id="@+id/nav_gallery"
        android:checkable="false"                  //line added here
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />
    </group>
</menu>


谢谢你的回答,但这会改变每件物品的颜色,对吗?因为我只需要改变一个项目的背景颜色你可以试试答案中添加的黑。希望能有帮助。这将更改该实例中选定项目的背景色。但是如果您不希望发生这种情况,您可以对除第一项之外的所有项目设置checkable=false。谢谢您的回答,但这会更改每个项目的颜色,对吗?因为我只需要改变一个项目的背景颜色你可以试试答案中添加的黑。希望能有帮助。这将更改该实例中选定项目的背景色。但如果您不希望发生这种情况,可以对除第一项之外的所有项设置checkable=false。