How to change color and font on ListView
I am trying to change my font(color and size) and the back ground on my ListView. I want to change it with code lines not on xml. my list view looks like: the xml:
<?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>
and my code is
public class NewsActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ArrayAdapter listItemAdapter = new ArrayAda开发者_运维技巧pter( 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();
what next? please give me an example base on my code
You need to create a 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;
}
}
The list item looks like this (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>
Use the TextView api's to decorate your text to your liking
and you will be using it like this
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
Create a CustomAdapter and in that you have the getView() so there if you want to change the listview background color use this :
v.setBackgroundColor(Color.CYAN);
If you want to change the textColor then do this :
tv.setTextColor(Color.RED);
and for the textSize :
tv.setTextSize(20);
where 'v' is the listview and 'tv' is the textview
Even better, you do not need to create separate android xml layout for list cell view. You can just use "android.R.layout.simple_list_item_1" if the list only contains textview.
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;
}
You can select a child like
TextView tv = (TextView)lv.getChildAt(0);
tv.setTextColor(Color.RED);
tv.setTextSize(12);
If u want to set background of the list then place the image before the < Textview>
< ImageView
android:background="@drawable/image_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and if u want to change color then put color code on above textbox like this
android:textColor="#ffffff"
If you just need to change some parameters of the View and the default behavior of ArrayAdapter its OK for you:
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;
}
}
If you want to use a color from colors.xml , experiment :
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));
...
}
You can use too:
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);
...
}
use them in Java code like this:
color = getResources().getColor(R.color.mycolor);
The getResources()
method returns the ResourceManager class for the current activity, and getColor()
asks the manager to look up a color given a resource ID
in android 6.0 you can change the colour of text like below
holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
精彩评论