इस प्रश्न को SO पर बहुत कुछ पूछा गया है, और मैंने सभी उत्तरों का संदर्भ दिया है। मैं अभी भी अपने नेविगेशन ड्रॉवर पर चयनित वस्तुओं के लिए डिफ़ॉल्ट होलो नीली पृष्ठभूमि के साथ छोड़ दिया गया है। मैं जावा में नया हूं और मैं .setAdapter()
के "संदर्भ" भाग के बारे में उलझन में हूं।
मेरी परियोजना एक एकल गतिविधि है जिसमें कई टुकड़े एनएवी ड्रॉवर का उपयोग करके बदल दिए जाते हैं।चयनित आइटम के लिए कस्टम पृष्ठभूमि रंग "सक्रिय बैकग्राउंड इंडिकेटर" नेविगेशन ड्रॉवर
mDrawerListView.setAdapter(new ArrayAdapter<String>(
// First parameter - Context
getActionBar().getThemedContext(),
// Second parameter - Layout for the row
R.layout.fragment_navigation_drawer_list_item,
// Third parameter - ID of the TextView to which the data is written
android.R.id.text1,
// Forth - the Array of data
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
संदर्भ यहाँ एंड्रॉयड स्टूडियो में "पूर्व पकाया" नेविगेशन ड्रॉर से आता है:
यहाँ मेरी अनुकूलक है। मैंने सोचा कि यह जवाब Navigation Drawer item background colour for selected item होगा। तो मैंने अपना संदर्भ getActivity().getBaseContext(),
पर बदल दिया, लेकिन इससे कुछ भी नहीं बदला।
मेरे विषय (styles.xml
):
<resources>
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/ActionBar</item>
</style>
<!-- Navigation Drawer styling -->
<style name="NavDrawerItemSelected" parent="AppBaseTheme">
<item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
</resources>
activated_background
'ड्रॉएबल' dir में:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/green" />
<item android:state_selected="true" android:drawable="@color/green" />
<item android:state_pressed="true" android:drawable="@color/green" />
<item android:state_checked="true" android:drawable="@color/green" />
<item android:drawable="@android:color/transparent" />
</selector>
मैं नहीं जानता कि जो उन राज्यों के ऊपर माना जाता है प्रयोग की जाने वाली है, इसलिए मैं जोड़ा उन सभी को मैं मिल सकता था।
अंत में, जब कोई आइटम चुना जाता है mDrawerListView.setItemChecked(position, true);
कहा जाता है।
कस्टम थीम स्टाइल को छोड़कर सबकुछ काम करता है। (न्यूनतम एपीआई = 11, एपीआई 17 एवीडी पर परीक्षण)
धन्यवाद! मुझे विश्वास नहीं है कि मैं अन्य उत्तरों में उस पर नहीं आया था। मैंने पहले 'एंड्रॉइड: पृष्ठभूमि =? एंड्रॉइड: सक्रिय बैकग्राउंड इंडिकेटर 'था। – Flash
आखिरकार! धन्यवाद आदमी .. –
यह एसओ पर इस क्यू का सबसे सरल और स्पष्ट उत्तर है। – u2tall