2011-09-09 44 views
18

我正在嘗試更改我的字體(顏色和大小)以及我的ListView上的背景。我想用不在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

我想在創建它們之前更改顏色和大小。 I \t int childCount = lv.getChildCount();但得到0.如何修復它 –

回答

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的API對你的文字裝點自己的喜好

,你將使用它像這樣

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList); 
mListView.setAdapter(listAdapter); 
+0

非常感謝!!!! –

+0

我嘗試了你在這裏給出的代碼...它的工作很好... 我想要定製的一件事是,如果position等於0,那麼我只需要爲該textview更改顏色。它的工作,但如果我滾動的建議,其他textview的顏色也改變了。我能做些什麼來設置僅位置1的textview的顏色? – Pratik

+1

如果你添加泛型到這個適配器,它會好得多 –

0

您可以選擇喜歡

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

嘗試過,但在電視=(TextView)lv.getChildAt(i); –

0

在這樣的Java代碼中使用它們:

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

getResources()方法返回當前活動ResourceManager類和getColor()詢問經理來查找顏色給定資源ID

7

創建CustomAdapter和你有getView()所以,如果要更改列表視圖背景顏色使用:如果你想改變文字顏色,然後做這個

v.setBackgroundColor(Color.CYAN); 

tv.setTextColor(Color.RED); 

,爲TEXTSIZE:

tv.setTextSize(20); 

其中「V」是ListView和「TV」是TextView的

1

如果妳希望設置列表的背景然後將<的TextView之前的圖像>

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

和如果u想改變顏色,然後把顏色代碼上面的文本框這樣

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

更妙的是,你並不需要創建列表單元格視圖單獨的Android XML佈局。如果列表只包含textview,則可以使用「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));