【问题标题】:How can I change the default blue color of a selected item in Navigation drawer如何更改导航抽屉中所选项目的默认蓝色
【发布时间】:2015-01-22 22:18:29
【问题描述】:

我尝试使用 android:listSelector,但它仅在我单击 listview 单元格时才有效,如何更改选定单元格的突出显示颜色,以便单元格保持以该颜色突出显示? 非常感谢。

highlight in blue

highlight in orange

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    将此添加到您用来显示列表视图元素的布局中

    android:background="?android:attr/activatedBackgroundIndicator" 
    

    创建一个文件 res/drawable/my_background.xml,内容如下:

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

    用您选择的颜色替换@color/blue。 然后,在您的主题中,添加以下行:

    <item name="android:activatedBackgroundIndicator">@drawable/my_background</item>
    

    【讨论】:

    • 我不知道为什么,但这对我不起作用..我认为它应该
    • 选中时如何改变导航栏的文字颜色?
    • 将此添加到选择器
    【解决方案2】:

    在这种情况下,您可能需要实现自己的适配器,并返回背景颜色设置为“橙色突出显示”的自定义布局。像这样的:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View your_custom_view = null;
    
            if (convertView == null) {
                your_custom_view = LayoutInflater.from(context).inflate(R.layout.your_custom_view_layout, null);
            } else {
                your_custom_view = convertView;
            }
    
            return your_custom_view;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      相关资源
      最近更新 更多