How to change font color and font type parameters of Android ListView

Source: Internet
Author: User

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.