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

时间:2014-05-27 17:56:24

标签: android

嗨,谢谢你的阅读。

我的Android应用程序导航抽屉有问题我无法改变蓝色的颜色 - 我已经过了所有其他问题来自SO,提到了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>

styles.xml

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

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小时了,这让我发疯了。对于我的好奇心告诉我实施的这种愚蠢的小变化,已经变成了一个研究项目。谁能看到我做错了什么?

2 个答案:

答案 0 :(得分:19)

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

接下来,我会仔细检查你是否已完成以下所有步骤:

  • 在您的应用主题中,尝试添加

<强>的themes.xml

<style name="Theme.mytheme" parent="android:Theme.Holo">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
  • drawable应该引用一个包含选择器的文件(就像你有的那样)

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

<resources>
    <item name="my_color" type="color">#ff0000</item>
</resources>
  • 最后,请确保使用
  • 在清单的应用程序代码中应用主题

<强>的AndroidManifest.xml

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

答案 1 :(得分:2)

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

在自定义适配器中:

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

在colors.xml中

    <resources>
        <color name="white">#ffffff</color>
        <color name="normal">#ef3272</color>
   </resources>
相关问题