2014-04-16 7 views
11

इस प्रश्न को 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 एवीडी पर परीक्षण)

उत्तर

24

मैंने इस सटीक समस्या में भाग लिया है। मुद्दा यह है कि R.layout.fragment_navigation_drawer_list_item की पृष्ठभूमि को आपके ड्रायबल में बदलने की आवश्यकता है। लेआउट में बस android:background="@drawable/activated_background" जोड़ें।

+0

धन्यवाद! मुझे विश्वास नहीं है कि मैं अन्य उत्तरों में उस पर नहीं आया था। मैंने पहले 'एंड्रॉइड: पृष्ठभूमि =? एंड्रॉइड: सक्रिय बैकग्राउंड इंडिकेटर 'था। – Flash

+0

आखिरकार! धन्यवाद आदमी .. –

+1

यह एसओ पर इस क्यू का सबसे सरल और स्पष्ट उत्तर है। – u2tall

9

मैं इस सवाल के लिए किसी भी पूर्ण स्पष्ट जवाब खोजने के लिए सक्षम नहीं था, इसलिए यहाँ यह है:

चरण 1. निर्दिष्ट करें सूची आइटम लेआउट अपने एडाप्टर का उपयोग करेगा, इस उदाहरण हम निर्दिष्ट कर रहे हैं में: R.layout.fragment_navigation_drawer_list_item

तो जैसा:

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), 
     }))` 

चरण 2. बनाएँ और एक drawable तो जैसे एक चयनकर्ता होगा कि निर्दिष्ट करने के लिए fragment_navigation_drawer_list_item.xml अनुकूलित: android:background="@drawable/activated_background" पूर्ण उदाहरण:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:gravity="center_vertical" 
    android:paddingStart="?android:attr/listPreferredItemPaddingStart" 
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" 
    android:minHeight="?android:attr/listPreferredItemHeightSmall" 
    android:background="@drawable/activated_background" /> 

चरण 3. बनाएँ और सिर्फ सवाल में की तरह अपने activated_background.xml फ़ाइल में चयनकर्ता को अनुकूलित, यह इस तरह दिखेगा:

<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> 
संबंधित मुद्दे