Android 更改导航抽屉中选定项目的颜色(从默认蓝色)

Android 更改导航抽屉中选定项目的颜色(从默认蓝色),android,Android,嗨,谢谢你的阅读 我的android应用程序导航抽屉有个问题,我无法将颜色从蓝色更改为蓝色-我已经检查了所有其他问题,参考了android文档,并尝试了迄今为止的一切。。。但还是没有运气。我真的希望有人能帮忙 迄今为止的守则: my_background.xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Blue still showing up! -->

嗨,谢谢你的阅读

我的android应用程序导航抽屉有个问题,我无法将颜色从蓝色更改为蓝色-我已经检查了所有其他问题,参考了android文档,并尝试了迄今为止的一切。。。但还是没有运气。我真的希望有人能帮忙

迄今为止的守则:

my_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Blue still showing up! -->
    <item android:state_activated="true" android:drawable="@color/lightPink" />
    <item android:state_selected="true" android:drawable="@color/lightPink" />
    <item android:state_pressed="true" android:drawable="@color/lightPink" />
    <item android:state_focused="true" android:drawable="@color/lightPink" />
    <item android:drawable="@color/lightPink" />

</selector>
<item name="android:activatedBackgroundIndicator">@drawable/my_background</item>
<style name="Theme.mytheme" parent="android:Theme.Holo">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/my_color" android:state_activated="true" />
    <item android:drawable="@android:color/transparent" />
</selector>
<resources>
    <item name="my_color" type="color">#ff0000</item>
</resources>
android:theme="@style/Theme.mytheme"

styles.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Blue still showing up! -->
    <item android:state_activated="true" android:drawable="@color/lightPink" />
    <item android:state_selected="true" android:drawable="@color/lightPink" />
    <item android:state_pressed="true" android:drawable="@color/lightPink" />
    <item android:state_focused="true" android:drawable="@color/lightPink" />
    <item android:drawable="@color/lightPink" />

</selector>
<item name="android:activatedBackgroundIndicator">@drawable/my_background</item>
<style name="Theme.mytheme" parent="android:Theme.Holo">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/my_color" android:state_activated="true" />
    <item android:drawable="@android:color/transparent" />
</selector>
<resources>
    <item name="my_color" type="color">#ff0000</item>
</resources>
android:theme="@style/Theme.mytheme"
@drawable/my\u背景
fragment_navigation_drawer.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:listSelector="@drawable/my_background"
    tools:context="com.randompractice.app.NavigationDrawerFragment"

/>


我已经被困在这个问题上24小时了,这让我发疯。因为我的好奇心让我去实施这样一个愚蠢的小改变,它已经变成了一个研究项目。有人能看出我做错了什么吗?

首先,我会尝试删除android:listSelector属性,因为我认为这是不必要的

接下来,我将再次检查您是否具备所有这些步骤:

  • 在应用程序的主题中,尝试添加
themes.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Blue still showing up! -->
    <item android:state_activated="true" android:drawable="@color/lightPink" />
    <item android:state_selected="true" android:drawable="@color/lightPink" />
    <item android:state_pressed="true" android:drawable="@color/lightPink" />
    <item android:state_focused="true" android:drawable="@color/lightPink" />
    <item android:drawable="@color/lightPink" />

</selector>
<item name="android:activatedBackgroundIndicator">@drawable/my_background</item>
<style name="Theme.mytheme" parent="android:Theme.Holo">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/my_color" android:state_activated="true" />
    <item android:drawable="@android:color/transparent" />
</selector>
<resources>
    <item name="my_color" type="color">#ff0000</item>
</resources>
android:theme="@style/Theme.mytheme"

ListDrawer-McClickListener:

    final CustomListAdapter myadapter;
     mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
            myadapter.setSelectedItem(position);
        }
最终自定义列表适配器myadapter;
mDrawerListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
选择项目(位置);
myadapter.setSelectedItem(位置);
}
在自定义适配器中:

public class CustomListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] itemname;

    int mSelectedItem;

    public CustomListAdapter(Activity context, String[] itemname ) {
        super(context, R.layout.drawer_list_item, itemname);
        // TODO Auto-generated constructor stub

        this.context = context;
        this.itemname = itemname;

    }

    public void setSelectedItem(int selectedItem) {
        this.mSelectedItem = selectedItem;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.drawer_list_item, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.textitem);
        String iname = itemname[position];
        txtTitle.setText(iname);
        txtTitle.setTypeface(tf);
        if (position == mSelectedItem) {
            txtTitle.setTextColor(getContext().getResources().getColor(R.color.white));
        } else {
            txtTitle.setTextColor(getContext().getResources().getColor(R.color.normal));
        }
        return rowView;
    }
}
公共类CustomListAdapter扩展了ArrayAdapter{
私人最终活动背景;
私有最终字符串[]itemname;
int mSelectedItem;
公共CustomListAdapter(活动上下文,字符串[]itemname){
super(上下文、右布局、抽屉列表、项目名称);
//TODO自动生成的构造函数存根
this.context=上下文;
this.itemname=itemname;
}
公共无效设置selectedItem(int selectedItem){
this.mSelectedItem=selectedItem;
}
公共视图getView(最终整数位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.drawer\u list\u项,null,true);
TextView txtTitle=(TextView)rowView.findViewById(R.id.textitem);
字符串iname=itemname[位置];
txtTitle.setText(iname);
txtTitle.setTypeface(tf);
如果(位置==mSelectedItem){
setTextColor(getContext().getResources().getColor(R.color.white));
}否则{
setTextColor(getContext().getResources().getColor(R.color.normal));
}
返回行视图;
}
}
在colors.xml中

    <resources>
        <color name="white">#ffffff</color>
        <color name="normal">#ef3272</color>
   </resources>

#ffffff
#ef3272

这应该被选为正确答案,这是我在搜索过程中找到的解决这个问题的唯一解决方案。这是迄今为止这个问题的最佳答案。停止使用
android:listSelector
并这样做——它将为你节省数小时的撞墙时间。