自定义ListView列表样式

先上效果自定义ListView列表样式_第1张图片


其实实现的思路很简单,共3步,


1。自定义控件继承TextView。重写onDraw方法。

private Paint marginPaint;
	private Paint linePaint;
	private int pagerColor;
	private float margin;
	
	private void init(){
		Resources myResources=getResources();
		
		//创建在ondraw中使用画刷
		marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
		marginPaint.setColor(myResources.getColor(R.color.notepad_matgin));
		
		linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
		linePaint.setColor(myResources.getColor(R.color.notepad_lines));
		
		//获得页面背景色和边缘宽度
		pagerColor=myResources.getColor(R.color.notepad_paper);
		margin=myResources.getDimension(R.dimen.nptepad_margin);
	}

protected void onDraw(Canvas canvas) {
		init();
		
		canvas.drawColor(pagerColor);
		
		canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint);
		canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
		
		canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
		
		canvas.save();
		
		canvas.translate(margin, 0);
		
		super.onDraw(canvas);
		
		canvas.restore();
		
	}



2.设置layout

<com.example.to_do_list.widgets.MyTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myfEditTex"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="。。。。。。。。。。。" >
</com.example.to_do_list.widgets.MyTextView>


3.设定listView的样式

aa = new ArrayAdapter<String>(this,R.layout.listview_style,todoItems);
        
        
        todoListFragment.setListAdapter(aa);



你可能感兴趣的:(自定义ListView列表样式)