csh_34 2012-11-13 05:09 采纳率: 0%
浏览 4273
已采纳

改变列表视图ListView中的字体和背景

我想改变列表视图ListView中的字体(颜色和大小)和背景。我想用代码改变而不是想通过设置xml的属性实现。以下的list view看起来就像xml:

<?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">

我的代码是:

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

接下来再写什么代码呢?

  • 写回答

1条回答

  • hcg_21 2012-11-13 08:21
    关注

    你应该创建一个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;
        }
    
    }
    

    list item应该向这样(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>
    
    listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
    mListView.setAdapter(listAdapter);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作