首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ListView上更改颜色和字体

如何在ListView上更改颜色和字体
EN

Stack Overflow用户
提问于 2011-09-09 19:45:27
回答 8查看 109.1K关注 0票数 21

我正在尝试改变我的字体(颜色和大小)和我的ListView的背景。我想用代码行来改变它,而不是xml。我的列表视图如下所示: xml:

代码语言:javascript
复制
 <?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>

我的代码是

代码语言:javascript
复制
public class NewsActivity  extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

 // ArrayAdapter listItemAdapter = new ArrayAdapter( 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();

下一步呢?请根据我的代码给我一个例子

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-09-09 20:00:49

您需要创建一个CustomListAdapter。

代码语言:javascript
复制
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;
    }

}

列表项如下所示(custom_list.xml):

代码语言:javascript
复制
<?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应用编程接口根据您的喜好装饰文本

您将像这样使用它

代码语言:javascript
复制
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
票数 32
EN

Stack Overflow用户

发布于 2011-09-09 20:30:22

创建一个CustomAdapter,在其中您有getView(),所以如果您想要更改列表视图的背景颜色,请使用以下代码:

代码语言:javascript
复制
v.setBackgroundColor(Color.CYAN);

如果您想要更改textColor,请执行以下操作:

代码语言:javascript
复制
tv.setTextColor(Color.RED);

而对于textSize:

代码语言:javascript
复制
tv.setTextSize(20);

其中'v‘是列表视图,'tv’是文本视图

票数 7
EN

Stack Overflow用户

发布于 2013-06-21 09:04:57

更好的是,您不需要为列表单元格视图创建单独的android xml布局。如果列表只包含textview,你可以只使用"android.R.layout.simple_list_item_1“。

代码语言:javascript
复制
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;
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7361135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档