当前位置: 编程技术>移动开发
本页文章导读:
▪怎么定制自己的View 如何定制自己的View
关于如何定制自己的View,查了相关的文档也看了一些例子,现在把自己的理解总结一下,和大家分享。制定自己的View,一般要继承自某一类的View,然后完成构造方法,以.........
▪ 浏览资料 浏览文件
主类
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import an.........
▪ Crowdroid(多功能多重微博客户端) 1.4.0已公布 Crowdroid(多功能多重微博客户端) 1.4.0已发布
Crowdroid 1.4.0中功能增设及修正事项 ★ 与URL短缩功能(bit.ly)的协同工作 ★ 一齐关闭回复履历★ 与Simple IME的协同工作★ 在Crowdroid for Business.........
[1]怎么定制自己的View
来源: 互联网 发布时间: 2014-02-18
如何定制自己的View
关于如何定制自己的View,查了相关的文档也看了一些例子,现在把自己的理解总结一下,和大家分享。
制定自己的View,一般要继承自某一类的View,然后完成构造方法,以及对onDraw()的重写,构造方法里面涉及到一些自定义的属性,自定义的属性在res/values下新建一个attrs.xml中申明,如下所示:
在构造方法中属性的引用格式是:属性集合名_属性名,如这里要引用text_color属性,则是R.stylebale.MyView_text_color
接下来的重点就是自定义自己的View,先上代码:
接下来就是布局文件了。
差不多就这么多了,下面是程序运行后的结果:
关于如何定制自己的View,查了相关的文档也看了一些例子,现在把自己的理解总结一下,和大家分享。
制定自己的View,一般要继承自某一类的View,然后完成构造方法,以及对onDraw()的重写,构造方法里面涉及到一些自定义的属性,自定义的属性在res/values下新建一个attrs.xml中申明,如下所示:
arrts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView" >
<attr name="text_color" format="color" />
<attr name="text_size" format="dimension" />
</declare-styleable >
</resources>
在构造方法中属性的引用格式是:属性集合名_属性名,如这里要引用text_color属性,则是R.stylebale.MyView_text_color
接下来的重点就是自定义自己的View,先上代码:
package com.lee.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint myPaint;//画笔
private static final String BLOG = "http://xueyilee.iteye.com";
public MyView(Context context) {
super(context);
myPaint = new Paint();
}
/*
Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.xml布局文件有该View时就调用此构造方法,属性会传给attrs
*/
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
myPaint = new Paint();
//获得TypedArray接口容器,通过它可以检索自定义的属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);
//检索属性值,第二个参数是默认值,如果没有定义则返回默认值
int textColor = ta.getColor(R.styleable.MyView_text_color, 0xff0000);
float textSize = ta.getDimension(R.styleable.MyView_text_size, 30);
myPaint.setColor(textColor);
myPaint.setTextSize(textSize);
ta.recycle();//这个回收方法一定不要忘了
}
/*当View渲染它的内容的时候调用此方法*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
myPaint.setStyle(Style.FILL);//设置填充
myPaint.setAntiAlias(true);//消除锯齿
canvas.drawCircle(50, 50, 30, myPaint);
myPaint.setColor(Color.BLUE);
canvas.drawText(BLOG, 20, 100, myPaint);
}
}
接下来就是布局文件了。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
//这个命名空间部分千万不要忘了,它是自定义属性前缀
xmlns:lee="http://schemas.android.com/apk/res/com.lee.view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.lee.view.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content"
lee:text_color="#00ff00"
lee:text_size="20px"
/>
</LinearLayout>
差不多就这么多了,下面是程序运行后的结果:
[2] 浏览资料
来源: 互联网 发布时间: 2014-02-18
浏览文件
主类
directory_list.xml
file_row.xml
主类
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FileList extends ListActivity {
private List<String> items = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.directory_list);
fillWithRoot();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selectionRowID = (int) getSelectedItemId();
if (selectionRowID == 0) {
fillWithRoot();
} else {
File file = new File(items.get(selectionRowID));
if (file.isDirectory())
fill(file.listFiles());
else {
Dialog dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star).setTitle("Not a Directory").setMessage(
"That's a file, not a directory").setPositiveButton("Cancel", null).create();
dialog.show();
}
}
}
private void fillWithRoot() {
fill(new File("/").listFiles());
}
private void fill(File[] files) {
items = new ArrayList<String>();
items.add(getString(R.string.to_top));
for (File file : files)
items.add(file.getPath());
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_row, items);
setListAdapter(fileList);
}
}
directory_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"> <ListView android:id="@id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_files" /> </LinearLayout>
file_row.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
[3] Crowdroid(多功能多重微博客户端) 1.4.0已公布
来源: 互联网 发布时间: 2014-02-18
Crowdroid(多功能多重微博客户端) 1.4.0已发布
Crowdroid 1.4.0中功能增设及修正事项
★ 与URL短缩功能(bit.ly)的协同工作
★ 一齐关闭回复履历
★ 与Simple IME的协同工作
★ 在Crowdroid for Business(Android客户端)处推送的“字符限制”可以在服务器端任意修改(字符数)
★ 其他Bugs修正
下载 : http://www.anhuioss.com/cn/download.html
Crowdroid 1.4.0中功能增设及修正事项
★ 与URL短缩功能(bit.ly)的协同工作
★ 一齐关闭回复履历
★ 与Simple IME的协同工作
★ 在Crowdroid for Business(Android客户端)处推送的“字符限制”可以在服务器端任意修改(字符数)
★ 其他Bugs修正
下载 : http://www.anhuioss.com/cn/download.html
最新技术文章: