android 自定义命名空间
一、统一的用户界面是可以使得应用程序更友好。要做到用户界面的统一,我们就必须用到风格(style)和主题(theme)。
自定义一个View的方法步骤如下:
1、首先,在values文件夹下定义一个atts.xml的文件,描述自定义的控件的属性
在values/attrs.xml中:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TestView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="imgBackground" format="integer" />
<attr name="textPaddingLeft" format="dimension"/>
<attr name="textPaddingTop" format="dimension"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TestView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="imgBackground" format="integer" />
<attr name="textPaddingLeft" format="dimension"/>
<attr name="textPaddingTop" format="dimension"/>
</declare-styleable>
</resources>
2、其次,定义一个继承自View的类,如:TestView,使其实现View的方法
view plaincopy to clipboardprint?
package com.test.TestView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class TestView extends View {
private Paint mPaint;
private Context mContext;
private String mStr;
public TestView(Context context, AttributeSet attrs) {//构造方法;根据需要实现继承自View的方法
super(context, attrs);
mContext = context;
initMyView();
//对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义。
TypedArray params = context.obtainStyledAttributes(attrs,R.styleable.MyView);
//得到自定义控件的属性值。
int backgroudId = params.getResourceId(R.styleable.MyView_imgBackground, 0);
if (backgroudId != 0)
setBackgroundResource(backgroudId);
int textColor = params.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);
setTextColor(textColor);
float textSize = params.getDimension(R.styleable.MyView_textSize, 36);
setTextSize(textSize);
float paddingLeft = params.getDimension( R.styleable.MyView_textPaddingLeft, 41);
float paddingTop = params.getDimension(R.styleable.MyView_textPaddingTop, 21);
setPaddings(paddingLeft, paddingTop);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mStr != null) {
canvas.drawText(mStr, 30, 60, mPaint);
}
}
private void initMyView() {
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
}
private void setTextColor(int textColor) {
mPaint.setColor(0XFFAABBCC);
}
private void setTextSize(float textSize) {
mPaint.setTextSize(textSize);
}
void setText(String text) {
mStr = text;
}
private void setPaddings(float paddingLeft, float paddingTop) {
setPadding((int) paddingLeft, (int) paddingTop, 0, 0);
}
}
package com.test.TestView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class TestView extends View {
private Paint mPaint;
private Context mContext;
private String mStr;
public TestView(Context context, AttributeSet attrs) {//构造方法;根据需要实现继承自View的方法
super(context, attrs);
mContext = context;
initMyView();
//对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义。
TypedArray params = context.obtainStyledAttributes(attrs,R.styleable.MyView);
//得到自定义控件的属性值。
int backgroudId = params.getResourceId(R.styleable.MyView_imgBackground, 0);
if (backgroudId != 0)
setBackgroundResource(backgroudId);
int textColor = params.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);
setTextColor(textColor);
float textSize = params.getDimension(R.styleable.MyView_textSize, 36);
setTextSize(textSize);
float paddingLeft = params.getDimension( R.styleable.MyView_textPaddingLeft, 41);
float paddingTop = params.getDimension(R.styleable.MyView_textPaddingTop, 21);
setPaddings(paddingLeft, paddingTop);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mStr != null) {
canvas.drawText(mStr, 30, 60, mPaint);
}
}
private void initMyView() {
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
}
private void setTextColor(int textColor) {
mPaint.setColor(0XFFAABBCC);
}
private void setTextSize(float textSize) {
mPaint.setTextSize(textSize);
}
void setText(String text) {
mStr = text;
}
private void setPaddings(float paddingLeft, float paddingTop) {
setPadding((int) paddingLeft, (int) paddingTop, 0, 0);
}
}
3、然后,在Layout文件中应用该自定义的view,如下:
如在main.xml中
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.test.TestView.TestView
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:textColor="#FFFFFFFF"
app:textSize="40dip"
app:textPaddingLeft="40dip"
app:textPaddingTop="40dip"
app:imgBackground="@drawable/bg" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.test.TestView.TestView
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:textColor="#FFFFFFFF"
app:textSize="40dip"
app:textPaddingLeft="40dip"
app:textPaddingTop="40dip"
app:imgBackground="@drawable/bg" />
</LinearLayout>
说明:上述红色部分——xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"是首先声明命名空间。命名空间为app.路径是http://schemas.android.com/apk/res/这一部分是不变的,后面接的是R的路径:com.test.TestView.R。然后在自定义控件的xml描述中就可以这样使用app:value="true"。这样就实现了自定义控件的初始化赋值。
4、然后就是使用了,在自己的Activity 中
view plaincopy to clipboardprint?
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestView mTextView=(TestView)findViewById(R.id.TestView);
mTextView.setText("自定义的View");
}
}
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestView mTextView=(TestView)findViewById(R.id.TestView);
mTextView.setText("自定义的View");
}
}
5、另外:
以下内容转自:http://www.android123.com.cn/androidkaifa/591.html
对于Android系统的自定义View可能大家都熟悉了,对于自定义View的属性添加,以及Android的Layout的命名空间问题,很多网友还不是很清楚,今天Android123一起再带大家温习一下
view plaincopy to clipboardprint?
CwjView myView=new CwjView(context);
CwjView myView=new CwjView(context);
如果用于游戏或整个窗体的界面,我们可能直接在onCreate中setContentView(myView); 当然如果是控件,我们可能会需要从Layout的xml中声明,比如
view plaincopy to clipboardprint?
<cn.com.android123.CwjView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<cn.com.android123.CwjView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
当然,我们也可以直接从父类声明比如
view plaincopy to clipboardprint?
<View class="cn.com.android123.CwjView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<View class="cn.com.android123.CwjView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
上面我们仅用了父类View的两个属性,均来自android命名空间,而名称为layout_width或layout_height,我们自定义的控件可能有更多的功能,比如
view plaincopy to clipboardprint?
<cn.com.android123.CwjView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
cwj:age="22"
cwj:university="sjtu"
cwj:city="shanghai"
/>
<cn.com.android123.CwjView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
cwj:age="22"
cwj:university="sjtu"
cwj:city="shanghai"
/>
我们可以看到上面的三个属性,是我们自定义的。作为标准xml规范,可能还包含了类似 xmlns:android="http://schemas.android.com/apk/res/android" 这样的语句,对于定义完整的View,我们的命名空间为cwj,这里可以写为
xmlns:cwj=http://schemas.android.com/apk/res/cn.com.android123.cwjView 或
xmlns:cwj=http://schemas.android.com/apk/res/android 都可以。
对于定义的cwj命名空间和age、university以及city的三个属性我们如何定义呢? 在工程的res/values目录中我们新建一个cwj_attr.xml文件,编码方式为utf-8是一个好习惯,内容如下
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="CwjView">
<attr name="age" format="integer" />
<attr name="city" format="string" />
<attr name="university" format="string" />
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="CwjView">
<attr name="age" format="integer" />
<attr name="city" format="string" />
<attr name="university" format="string" />
</declare-styleable>
</resources>
这里我们可能对format不是很熟悉,目前Android系统内置的格式类型有integer比如ProgressBar的进度值,float比如
RatingBar的值可能是3.5颗星,boolean比如ToggleButton的是否勾选,string比如TextView的text属性,当
然除了我们常见的基础类型外,Android的属性还有特殊的比如color是用于颜色属性的,可以识别为#FF0000等类型,当然还有
dimension的尺寸类型,比如23dip,15px,18sp的长度单位,还有一种特殊的为reference,一般用于引用@+id/cwj
@drawable/xxx这样的类型。
当然什么时候用reference呢? 我们就以定义一个颜色为例子:
<attr name="red" format="color|reference" />
这里我们用了逻辑或的运算符,定义的红色是颜色类型的,同时可以被引用。当然,对于我们自定义的类中,我们需要使用一个名为
obtainStyledAttributes的方法来获取我们的定义。在我们自定义View的构造方法(Context context,
AttributeSet attrs)的重载类型中可以用
view plaincopy to clipboardprint?
public CwjView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.cwj_attr);
mAge = a.getInteger(R.styleable.CwjView_age, 22);
mCity = a.getString(R.styleable.CwjView_city, "shanghai");
mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");
a.recycle(); //Android123提示大家不要忘了回收资源
}
public CwjView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.cwj_attr);
mAge = a.getInteger(R.styleable.CwjView_age, 22);
mCity = a.getString(R.styleable.CwjView_city, "shanghai");
mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");
a.recycle(); //Android123提示大家不要忘了回收资源
}
这样类的全局成员变量 mAge、mCity就获取了我们需要的内容,当然根据layout中的数值我们自定义的CwjView需要动态的处理一些数据的情况,可以使用AttributeSet类的getAttributeResourceValue方法获取。
view plaincopy to clipboardprint?
public CwjView(Context context, AttributeSet attrs){
super(context, attrs);
resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "age", 100);
resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "city", "shanghai");
//resID就可以任意使用了
public CwjView(Context context, AttributeSet attrs){
super(context, attrs);
resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "age", 100);
resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "city", "shanghai");
//resID就可以任意使用了
}
以上两种方法中,参数的最后一个数值为默认的
android 自定义命名空间的更多相关文章
- android 自定义命名空间 http://schemas.android.com/apk/res-auto
XML中用 xmlns="http://schemas.android.com/apk/res-auto" 获取自定义属性值: public static String NAMES ...
- Android 自定义 attr
好纠结,弄了一个下午老是报错如是总结一下安卓自定视图和自定义属性. (一)自定义属性 在Values文件下建立一个attrs.xml文件,attr的format可以参考:http://www.cnbl ...
- Android 自定义View及其在布局文件中的使用示例
前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...
- (转)android自定义组合控件
原文地址:http://mypyg.iteye.com/blog/968646 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 1.控件布局:以Linea ...
- Android 自定义view(二) —— attr 使用
前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...
- [原] Android 自定义View步骤
例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ...
- Android自定义View
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...
- Android自定义视图教程
Android自定义视图教程 Android的UI元素都是基于View(屏幕中单个元素)和ViewGroup(元素的集合),Android有许多自带的组件和布局,比如Button.TextView.R ...
- Android 自定义View (一)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...
随机推荐
- jackson 学习笔记
Jackson以优异的解析性能赢得了好评,今天就看看Jackson的一些简单的用法. Jackson使用之前先要下载,这里一共有三个jar包,想要获得完美的Jackson体验,这三个jar包都不可或缺 ...
- html响应式布局,css响应式布局,响应式布局入门
html响应式布局,css响应式布局,响应式布局入门 >>>>>>>>>>>>>>>>>>& ...
- C语言基础知识小总结(1)
这几天在学习C语言,零零散散的学了十来天,这两天由于家里来了朋友,也没有顾得上写个总结,今天刚把朋友送走,下面就把这十来天的学习情况总结一下,一边在以后好复习与查看. 一.流程控制包括:顺序语句.判断 ...
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
转载自: http://blog.csdn.net/hulihui/article/details/3244520 原文:How to use the SocketAsyncEventArgs c ...
- OC加强-day02
#program mark - 01 @class关键字 [掌握] 1.当两个头文件互相引用的时候,如果双方都是用#import来引入对方的头文件,就会造成死循环,编译不通过 解决方案:其中一边不要使 ...
- 使用Reveal.app调试整个项目UI时间,增加LD指令 -Objc引起项目中多个静态库冲突问题
今天接触到一个新的UI调试工具教程如下: iOS真机UI调试利器——Reveal 引入增加-ObjC -framework Reveal指令后,发现项目出现多重静态库冲突问题, 首先介绍一个指令: - ...
- 【BZOJ1875】【矩阵乘法】[SDOI2009]HH去散步
Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走回. 又因 ...
- 用frame实现最基本的上中下三层布局,中间又分左右两部分.
用frame实现最基本的上中下三层布局,中间又分左右两部分. 用frame的好处在于不用象DIV一样要对浮动和大小进行精确控制,以及要考虑宽屏的时候怎么办.而且在导航的时候非常简单.比如说,左边是导航 ...
- css3 iphone开关 移动端开关、按钮、input
css3 iphone开关 移动端开关.按钮.input <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- TensorFlow和最近发布的slim
笔者将和大家分享一个结合了TensorFlow和最近发布的slim库的小应用,来实现图像分类.图像标注以及图像分割的任务,围绕着slim展开,包括其理论知识和应用场景. 之前自己尝试过许多其它的库,比 ...