The default ListView control of Android does not support changing the font color or font Style.
If you want to implement this function, you need to write some code yourself.
Step 1:
Define the resource xml of A ListView item and put it in the res/layout/directory, for example, list_item1.xml. The content is:
<? Xml version = "1.0" encoding = "UTF-8"?>
<TextView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/text1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textAppearance = "? Android: attr/textAppearanceLarge"
Android: textColor = "# 0000ff"
Android: testStyle = "italic"
Android: gravity = "center_vertical"
Android: paddingLeft = "6dip"
Android: minHeight = "? Android: attr/listPreferredItemHeight"
/>
The text above is in blue and displayed in italic.
Step 2:
Change the program:
NameAdapter = new ArrayAdapter <String> (this, R. layout. list_item1, nameList );
Refer to the xml file in frameworks \ base \ core \ res \ layout \ of the Android source code.
File. You can check it out and learn many useful things.
The above method implements static changes, but cannot be changed dynamically.
Second question:
How to achieve dynamic changes, dynamic changes need to implement Adapter
Private String strArr [] = {"Shenzhen", "Beijing", "Shanghai", "Hangzhou", "Nanjing", "Guangzhou "};
MyListView. setAdapter (new MyAdapter ());
Private class MyAdapter extends BaseAdapter
{
@ Override
Public int getCount ()
{
Return strArr. length;
}
@ Override
Public Object getItem (int arg0 ){
Return arg0;
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// Position indicates that the position starts from 0 and convertView is a Spinner.
// Usually the return view is convertView
// Parent is the parent form, that is, the Spinner, ListView, and GridView.
TextView mTextView = new TextView (getApplicationContext ());
MTextView. setText (strArr [position]);
MTextView. setTextSize (15 );
Return mTextView;
/* // Or use code similar to the following...
If (convertView = null ){
ConvertView = mLayoutInflater. inflate (R. layout. item1, parent, false );
}
MView = (TextView) convertView. findViewById (R. id. text1 );
MView. setText (...);
MView. setTextColor (...);
Return convertView;
*/
}
}
In fact, for the Spinner, The ListView requires adapter programming to achieve the desired effect of self-painting through this method.
From maojudong's column