java - 我怎样才能改变我的 ListView 的字体颜色和字体类型

标签 java android xml

我有一个包含 TextView 、按钮、微调器和 ListView 的 Activity
我的主要 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);

但我无法在 ListView 或微调器中更改字体颜色或字体类型。我该怎么做?

最佳答案

您需要创建一个 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);

关于java - 我怎样才能改变我的 ListView 的字体颜色和字体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216180/

相关文章:

java - GWT Java - CellTable - ButtonCell - 如何使其响应点击

android - window : The "android" command is deprecated

java - 如何在android编程中获取sqlite数据库中的一行或一列的文本?

android - 基于更改并存储到 map 中的 DataSnapshot 出错

java - 获取 JSON 弹出窗口以保存或打开对话框而不是实际内容?

java - 从 BitSet 转换为字节数组

java - 适用于 Android 的 BBC iPlayer 广播应用程序 - 滚轮导航

xml - Visual Studio 2010 中的 XSLT 2.0 intellisense - 添加架构?

PHP Mailer 仅在添加打印到 xml 文件的功能后才发送重复的电子邮件

python - 修改python中的xml数据