简体   繁体   English

更改导航抽屉所选项目颜色为默认蓝色

[英]Changing Navigation Drawer Selected Item Color from default blue

Hi and thanks for reading. 嗨,谢谢你的阅读。

I have a problem with my android apps navigation drawer where I cannot change the color from blue - I have went over all the other questions from SO, referred to the android documentation, and have tried everything so far... but still no luck. 我有一个问题,我的Android应用程序导航抽屉,我无法改变蓝色的颜色 - 我已经过了所有其他问题从SO,参考Android文档,并尝试了一切到目前为止...但仍然没有运气。 I really hope someone can help. 我真的希望有人可以提供帮助。

The code so far: 到目前为止的代码:

my_background.xml 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>

styles.xml styles.xml

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

fragment_navigation_drawer.xml 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"

/>

I have been stuck on this for 24 hours now and it is driving me crazy. 我现在已经被困在这24小时了,这让我发疯了。 For such a silly little change that my curiosity told me to implement, has turned into a research project. 对于我的好奇心告诉我实施的这种愚蠢的小变化,已经变成了一个研究项目。 Can anybody see what I am doing wrong? 谁能看到我做错了什么?

First, I would try removing the android:listSelector attribute, as I don't believe it is necessary. 首先,我会尝试删除android:listSelector属性,因为我认为没有必要。

Next, I would double check you have all these steps: 接下来,我会仔细检查您是否已完成以下所有步骤:

  • In your application's theme, try adding 在您的应用程序主题中,尝试添加

themes.xml 的themes.xml

<style name="Theme.mytheme" parent="android:Theme.Holo">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
  • The drawable should refer to a file containing a selector like (like you have) drawable应该引用一个包含选择器的文件(就像你一样)

activated_background.xml activated_background.xml

<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>

colors.xml colors.xml

<resources>
    <item name="my_color" type="color">#ff0000</item>
</resources>
  • Finally, make sure you are applying the theme in your manifest's application tag using 最后,确保使用清单的应用程序标记中应用主题

AndroidManifest.xml AndroidManifest.xml中

android:theme="@style/Theme.mytheme"

ListDrawer onItemClickListener: ListDrawer onItemClickListener:

    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);
        }

In custom adapter: 在自定义适配器:

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;
    }
}

In colors.xml 在colors.xml中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM