簡體   English   中英

更改導航抽屜所選項目顏色為默認藍色

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

嗨,謝謝你的閱讀。

我有一個問題,我的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小時了,這讓我發瘋了。 對於我的好奇心告訴我實施的這種愚蠢的小變化,已經變成了一個研究項目。 誰能看到我做錯了什么?

首先,我會嘗試刪除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"

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM