好贷网好贷款

ListView记事本效果

发布时间:2016-12-4 18:21:12 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"ListView记事本效果",主要涉及到ListView记事本效果方面的内容,对于ListView记事本效果感兴趣的同学可以参考一下。

1.创建颜色colors.xml<?xml version="1.0" encoding="utf-8"?> <resources> <color name="notepad_paper">#AAFFFF99</color> <color name="notepad_lines">#FF0000FF</color> <color name="notepad_margin">#90FF0000</color> <color name="notedpad_text">#AA0000FF</color> </resources> 2.创建尺寸dimens.xml <?xml version="1.0" encoding="utf-8" ?> <resources> <dimen name="notepad_margin">30px</dimen> </resources> 3.写activity布局 activity_main <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="布局测试" /> <ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 4.写listview样式,这里只加一个TextView,TodoListItemView是修改了TextView布局,这样可以显示记事本效果 <?xml version="1.0" encoding="utf-8"?> <com.example.text.TodoListItemView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:scrollbars="vertical" android:textColor="@color/notedpad_text" android:fadingEdge="vertical" > </com.example.text.TodoListItemView> 5.主要部分,修改TextView样式 public class TodoListItemView extends TextView { private Paint marginPaint; private Paint linePaint; private int paperColor; private float margin; public TodoListItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public TodoListItemView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public TodoListItemView(Context context) { super(context); init(); } private void init(){ //获得对资源表的引用 Resources myResource=getResources(); //创建绘图刷 marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG); marginPaint.setColor(myResource.getColor(R.color.notepad_margin)); linePaint =new Paint(Paint.ANTI_ALIAS_FLAG); linePaint.setColor(myResource.getColor(R.color.notepad_lines)); //获得页面背景色和边缘宽度 paperColor=myResource.getColor(R.color.notepad_paper); margin=myResource.getDimension(R.dimen.notepad_margin); } public void onDraw(Canvas canvas){ canvas.drawColor(paperColor); //绘制规则的直线 canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint); canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint); //绘制边缘 canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint); //移动文本,让它跨国边缘 canvas.save(); canvas.translate(margin, 0); //使用TextView渲染文本 super.onDraw(canvas); canvas.restore(); } } 6.Activity代码 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listview=(ListView)findViewById(R.id.myListView); EditText et=(EditText)findViewById(R.id.myEditText); final ArrayList<String> todoItems=new ArrayList<String>(); String[] s={"第一行","第二行","第三行","第四行","第五行"}; for(int i=0;i<s.length;i++){ todoItems.add(s[i]); } int resId=R.layout.main; final ArrayAdapter<String> aa=new ArrayAdapter<String>(this,resId,todoItems); listview.setAdapter(aa); } }

上一篇:html学习几小点2
下一篇:AutoMapper对象映射工具

相关文章

相关评论