如何更改listview的字体颜色和字体类型

时间:2014-06-14 02:51:37

标签: java android xml

我有一个活动包含文本视图,按钮,微调器和列表视图
我的主XML文件包含:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
tools:context="com.example.taxitabriz.MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="8dp"
    android:text="@string/app_name"
    android:textSize="35dp"
    android:textStyle="bold"
    android:textColor="#996600"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/linearlayout1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#996600"
        android:text="@string/exit"
        android:background="@drawable/backbutton" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#996600"
        android:text="@string/about"
        android:background="@drawable/backbutton" />

</LinearLayout>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="19dp" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/spinner1"
    android:layout_above="@+id/linearlayout1"
    android:layout_alignParentRight="true"
    android:layout_weight="49.94" >
</ListView>


我的部分Java代码在这里:

ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
    sp.setAdapter(ard);
    lv1=(ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
    lv1.setAdapter(ard1);

但我无法在列表视图或微调器中更改字体颜色或字体类型。我怎么能这样做?

5 个答案:

答案 0 :(得分:2)

您需要创建一个CustomListAdapter。

private class CustomListAdapter extends ArrayAdapter {

    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);

答案 1 :(得分:1)

如果您真的想使用androids简单列表布局,we see that they declare their textview's identifier as such

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/dropDownItemStyle"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

所以,只需制作自定义适配器,扩充其布局和放大器即可。找到该文本视图的ID。如下所示:

    public final class CustomAdapter extends ArrayAdapter<String> {  

    private Context context;
    ViewHolder holder; 
    private ArrayList<String> messages;


    public CustomAdapter(Context context, ArrayList<String> data) {

        this.context = context;
        this.messages = data;
        Log.d("ChatAdapter", "called constructor");

    }

    public int getCount() {
        return messages.size();
    }


    public View getView(int position, View convertView, ViewGroup viewGroup) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.simple_dropdown_item_1line, null);

            holder = new ViewHolder();
            holder.message = (TextView) convertView
                    .findViewById(R.id.text1);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        holder.message.setText(data.get(position));
        holder.message.setTextColor(Color.BLUE);

        // don't do this, remember the face variable once just showing explicitly for u
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); 

        holder.message.setTypeface(face); 
        return convertView;
    }

    public static class ViewHolder {
        public TextView message;
    }
}

编辑:以下是您在活动中使用自定义数组适配器的方法。

// I'm not sure if your sp or lv1 wants the adapter, but
// whatever you require your list view's data to be just 
// set the custom adapter to it
CustomAdapter<String> myAdapter = new ArrayAdapter<String>(this, s1);
lv1=(ListView) findViewById(R.id.listView1)
lv1.setAdapter(myAdapter);

答案 2 :(得分:0)

你可以这样做,在res/layout/list_item1.xml

的xml中添加一个ListView项
<?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"

/>

然后,

ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);// **change R.layout.list_item1**
lv1.setAdapter(ard1);

答案 3 :(得分:0)

你必须将.ttf文件的字体(你想要添加)放在 asstes / fonts / 文件夹中,并在onCreate方法中编写代码,如下所示。我在 asstes / fonts / 文件夹中输入了 Chalkduster.ttf

TextView txttest = (TextView) findViewById(R.id.txttest);
Typeface custom_font = Typeface.createFromAsset(getAssets(),
            "fonts/Chalkduster.ttf");
txttest.setTypeface(custom_font);

答案 4 :(得分:0)

您可以通过编写代码来处理所选项目的点击次数,如下所示

ListView lvNews = (ListView) findViewById(R.id.lvNews);
lvNews.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
          // Write your code here    
            //int newsId = newsArray.get(position).getId();
            //Intent i = new Intent(NewsListActivity.this,
            //      NewsDetailActivity.class);
            //i.putExtra(Constants.NEWS_ID, newsId);
            //i.putExtra("isFromNotification", false);
            //startActivity(i);

        }

    });