您的位置:首页 > 移动开发 > Android开发

自定义android控件——TextView 并添…

2013-12-19 20:40 399 查看
自定义android控件——TextView
并添加到ListView中
①在values文件夹下定义TextView的颜色和边距规则:

颜色规则:notepadcolors.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="notepad_text">#AA0000FF</color>
</resources>

边距规则:notepaddimens.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<dimen name="notepad_margin">30dp</dimen>

</resources>

②创建一个扩展原TextView的新类,并重写onDraw方法来修改视图的外观

package com.yinger;

import android.content.Context;

import android.content.res.Resources;

import android.graphics.Canvas;
import android.graphics.Paint;

import android.util.AttributeSet;

import android.widget.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);

// TODO Auto-generated constructor stub

init();

}

public TodoListItemView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public TodoListItemView(Context context) {

super(context);

init();

}

private void init() {

Resources myResources = getResources();

// Create the paint brushes we will use in the onDraw method.

marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

marginPaint.setColor(myResources.getColor(R.color.notepad_margin));

linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

linePaint.setColor(myResources.getColor(R.color.notepad_lines));

// Get the paper background color and the margin width.

paperColor = myResources.getColor(R.color.notepad_paper);

margin = myResources.getDimension(R.dimen.notepad_margin);

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

// Color as paper

canvas.drawColor(paperColor);

// Draw ruled lines

canvas.drawLine(0, 0, 30.0f, 0, linePaint);

canvas.drawLine(0, getMeasuredHeight(),

getMeasuredWidth(), getMeasuredHeight(),

linePaint);

// Draw margin

canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);

// Move the text across from the margin

canvas.save();

canvas.translate(margin, 0);

// Use the TextView to render the text.

super.onDraw(canvas);

canvas.restore();

}

}

注:要重写所有的构造方法,这样比较保险。

-----------------------------------------------------------我是分隔线-------------------------------------------------------------------

OK,以上,已经实现了TextView的自定义,下面我们看看怎么来引用它(以ListView为例)

③在layout文件夹下创建list item 的布局规定:

todolist_item.xml

<?xml version="1.0" encoding="utf-8"?>

<com.yinger.TodoListItemView

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

android:scrollbars="vertical"

android:textColor="@color/notepad_text"

android:fadingEdge="vertical"

/>
注意,黄色部分是重点。

④大家熟悉的布局文件,我这里是main.xml,没啥好说的。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<EditText

android:id="@+id/myEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="New To Do Item"

/>

<ListView

android:id="@+id/myListView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>
</LinearLayout>
⑤和以前一样的定义ListView,没啥区别:

package com.yinger;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;
import android.view.KeyEvent;

import android.view.View;
import android.view.View.OnKeyListener;

import android.widget.ArrayAdapter;

import android.widget.EditText;
import android.widget.ListView;

public class TextViewDemo extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Get references to UI widgets

ListView myListView = (ListView)findViewById(R.id.myListView);

final EditText myEditText = (EditText)findViewById(R.id.myEditText);

final ArrayList<String> todoItems = new ArrayList<String>();

int resID = R.layout.todolist_item;

final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, resID,

todoItems);

myListView.setAdapter(aa);

myEditText.setOnKeyListener(new OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_DOWN)

if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)

{

todoItems.add(0, myEditText.getText().toString());

aa.notifyDataSetChanged();

myEditText.setText("");

return true;

}

return false;

}

});

}

}

运行结果截图:


并添加到ListView中" TITLE="自定义android控件鈥斺擳extView 并添加到ListView中" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: