2011-09-09 19 views
18

पर रंग और फ़ॉन्ट को कैसे बदला जाए, मैं अपने फ़ॉन्ट दृश्य (रंग और आकार) और मेरी सूची दृश्य पर बैक ग्राउंड बदलने की कोशिश कर रहा हूं। मैं इसे xml पर कोड लाइनों के साथ बदलना चाहता हूं। xml:: मेरी सूची दृश्य की तरह लग रहाListView

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dp" 
android:textSize="18sp" android:text="@string/hello"> 
</TextView> 

और मेरे कोड

public class NewsActivity extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

// ArrayAdapter listItemAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, v_itemList); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,ynetList)); 

     View v=getListView() ; 

     ListView lv = getListView(); 

आगे क्या है? मुझे मेरे कोड

+0

क्या आप सूची बनाते समय रंग और आकार सेट करने की कोशिश कर रहे हैं? या सूची बनाने के बाद इसे विशेष बच्चे को करें? – blessenm

+0

मैं उन्हें बनाए जाने से पहले रंग और आकार बदलना चाहता हूं। मैं \t int childCount = lv.getChildCount(); लेकिन –

उत्तर

30

आप एक CustomListAdapter बनाने की जरूरत एक बच्चे चुन सकते हैं।

public class CustomListAdapter extends ArrayAdapter <String> { 

    private Context mContext; 
    private int id; 
    private List <String>items ; 

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list) 
    { 
     super(context, textViewResourceId, list);   
     mContext = context; 
     id = textViewResourceId; 
     items = list ; 
    } 

    @Override 
    public View getView(int position, View v, ViewGroup parent) 
    { 
     View mView = v ; 
     if(mView == null){ 
      LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mView = vi.inflate(id, null); 
     } 

     TextView text = (TextView) mView.findViewById(R.id.textView); 

     if(items.get(position) != null) 
     { 
      text.setTextColor(Color.WHITE); 
      text.setText(items.get(position)); 
      text.setBackgroundColor(Color.RED); 
      int color = Color.argb(200, 255, 64, 64); 
       text.setBackgroundColor(color); 

     } 

     return mView; 
    } 

} 

सूची आइटम इस (custom_list.xml) की तरह दिखता है:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" 
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/> 
</LinearLayout> 

TextView एपीआई अपनी पसंद

अपने पाठ को सजाने के लिए उपयोग करें और आप इसे इस तरह

का उपयोग किया जाएगा
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList); 
mListView.setAdapter(listAdapter); 
+0

बहुत बहुत धन्यवाद !!!! –

+0

मैंने आपके द्वारा दिए गए कोड को आजमाया है ... यह अच्छा काम कर रहा है ... एक चीज जिसे मैं कस्टमाइज़ करना चाहता हूं वह यह है कि यदि स्थिति 0 के बराबर होती है तो मुझे केवल उस टेक्स्टव्यू के लिए रंग बदलना होगा। यह काम करता है लेकिन अगर मैं सुझावों को स्क्रॉल करता हूं, तो अन्य टेक्स्टव्यू रंग भी बदल जाता है। मैं केवल टेक्स्टव्यू का रंग सेट करने के लिए क्या कर सकता हूं जो स्थिति 1 पर है? – Pratik

+1

यदि आप इस एडाप्टर में जेनेरिक जोड़ते हैं, तो यह बहुत बेहतर है –

0

पर एक उदाहरण आधार दे कृपया आप की तरह

TextView tv = (TextView)lv.getChildAt(0); 
tv.setTextColor(Color.RED); 
tv.setTextSize(12);  
+2

इसे ठीक करने के लिए 0.how प्राप्त करें, लेकिन यह कोशिश की लेकिन टीवी = (टेक्स्टव्यू) lv.getChildAt (i) पर शून्य हो रही है; –

0

उन्हें जावा कोड में उपयोग करें:

color = getResources().getColor(R.color.mycolor); 

getResources() विधि वर्तमान गतिविधि के लिए संसाधनप्रबंधक श्रेणी देता है, और getColor()) प्रबंधक पूछता है एक रंग को देखने के लिए एक संसाधन आईडी

7

एक CustomAdapter बनाएं दिया और कहा कि आप getView (है तो वहाँ अगर

tv.setTextColor(Color.RED); 
:

v.setBackgroundColor(Color.CYAN); 

आप तो ऐसा करने textcolor बदलना चाहते हैं: आप बदलना सूचीदृश्य पृष्ठभूमि रंग इस का उपयोग करना चाहते 0

और textSize के लिए:

tv.setTextSize(20); 

जहां 'वी' सूचीदृश्य और 'टीवी' है TextView है

1

यू सूची की पृष्ठभूमि सेट करने के लिए तो < TextView से पहले छवि जगह चाहते हैं >

< ImageView 
android:background="@drawable/image_name" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"/> 

और यू रंग बदलना चाहते हैं तो इस

android:textColor="#ffffff" 
तरह ऊपर पाठ बॉक्स पर रंग कोड डाल
1

तुम सिर्फ देखें और आप के लिए अपने ठीक ArrayAdapter के डिफ़ॉल्ट व्यवहार के कुछ मानकों को बदलने की जरूरत है:

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 

public class CustomArrayAdapter<T> extends ArrayAdapter<T> { 

    public CustomArrayAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 

      // Here all your customization on the View 
      view.setBackgroundColor(.......); 
      ... 

     return view; 
    } 


} 
3

और भी बेहतर, आप सूची सेल दृश्य के लिए अलग एंड्रॉयड XML लेआउट बनाने की जरूरत नहीं है। यदि सूची में केवल टेक्स्टव्यू है तो आप केवल "android.R.layout.simple_list_item_1" का उपयोग कर सकते हैं।

private class ExampleAdapter extends ArrayAdapter<String>{ 

    public ExampleAdapter(Context context, int textViewResourceId, String[] objects) { 
     super(context, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 


     View view = super.getView(position, convertView, parent); 

     TextView tv = (TextView) view.findViewById(android.R.id.text1); 
     tv.setTextColor(0); 

     return view; 
    } 
1

आप colors.xml, प्रयोग से एक रंग का उपयोग करना चाहते हैं:

public View getView(int position, View convertView, ViewGroup parent) { 
     ... 
     View rowView = inflater.inflate(this.rowLayoutID, parent, false); 
     rowView.setBackgroundColor(rowView.getResources().getColor(R.color.my_bg_color)); 
     TextView title = (TextView) rowView.findViewById(R.id.txtRowTitle); 
     title.setTextColor(
      rowView.getResources().getColor(R.color.my_title_color)); 
     ... 
    } 

तुम भी उपयोग कर सकते हैं:

private static final int bgColor = 0xAAAAFFFF; 
public View getView(int position, View convertView, ViewGroup parent) { 
     ... 
     View rowView = inflater.inflate(this.rowLayoutID, parent, false); 
      rowView.setBackgroundColor(bgColor); 
... 
} 
0
एंड्रॉयड 6.0 में

आप बदल सकते हैं

holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context, R.color.green)); 
जैसे टेक्स्ट का रंग