Android declare-styleable自定义属性
2016-03-17 15:24
399 查看
我们自己定义view,通常继承View,重写构造方法和onDraw等函数,然后具体实现自己定义的复杂view。但是继承这个自定义view的有的属性又各有不同,有的属性在原生属性中没有,这时候就可以使用自定义的属性来便捷的设置相应的属性。
1、在res/values/下新建一个attrs.xml文件。写入<resources><declare-styleable><attr/>这三级
标签。并填入相应属性。
2、建立一个自定义视图类,本次偷懒直接继承自textview。然后使用typedarray类来获取控件相应的
属性并使用。
3、在布局文件里引用该类,并设定相应的属性。记得要定义xmlns:myapp(后面的名称随意取,但要上下一致。)= "http://schemas.android.com/apk/res/com.example.declarestyleabletest"(最后是该自定义视图类所在的包名)
补充一些常用的format
reference 指定某资源ID
string 字符串
dimension 尺寸
color 颜色
boolean 布尔
integer 整型
float 浮点型
fraction 百分数
enum 枚举
flag 位运算
xml 中定义好自己的属性值,在在代码中得到值,或使用默认值
public CText(Context context, AttributeSet set) {
super(context, set);
this.context = context;
initView();
TypedArray array = context.obtainStyledAttributes(set,R.styleable.CEditText);
initAttrs(array);
}
private void initAttrs(TypedArray a) {
hint = a.getString(R.styleable.CEditText_cHint);
text = a.getString(R.styleable.CEditText_cText);
setAttrs();
a.recycle();
}
转载请注明出处:http://blog.csdn.net/chenlinfeng772885775/article/details/50911702
1、在res/values/下新建一个attrs.xml文件。写入<resources><declare-styleable><attr/>这三级
标签。并填入相应属性。
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="myStyle"> <attr name="textSize" format="dimension" /> <attr name="backgroud" format="reference" /> <attr name="text" format="string|reference" /> <attr name="textColor" format="color" /> <attr name="yesOrNot" format="boolean" /> <attr name="number"> <enum name="one" value="0" /> <enum name="two" value="1" /> <enum name="leftToRight" value="2" /> <enum name="topToBottom" value="3" /> </attr> </declare-styleable> </resources></span>
2、建立一个自定义视图类,本次偷懒直接继承自textview。然后使用typedarray类来获取控件相应的
属性并使用。
<span style="font-size:18px;">import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.TextView; public class MyView extends TextView { public MyView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.myStyle); float textSize = typedArray.getDimension(R.styleable.myStyle_textSize, 15); Drawable back = typedArray.getDrawable(R.styleable.myStyle_backgroud); String textString = typedArray.getString(R.styleable.myStyle_text); int textColor = typedArray.getInt(R.styleable.myStyle_textColor, Color.BLACK); boolean yesOrNot = typedArray.getBoolean(R.styleable.myStyle_yesOrNot, true); int number = typedArray.getInt(R.styleable.myStyle_number, 0); setTextSize(textSize); setText(textString); setBackgroundDrawable(back); setTextColor(textColor); } } </span>
3、在布局文件里引用该类,并设定相应的属性。记得要定义xmlns:myapp(后面的名称随意取,但要上下一致。)= "http://schemas.android.com/apk/res/com.example.declarestyleabletest"(最后是该自定义视图类所在的包名)
<span style="font-size:18px;"><span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myapp="http://schemas.android.com/apk/res/com.example.declarestyleabletest" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.declarestyleabletest.MainActivity" tools:ignore="MergeRootFrame" > <com.example.declarestyleabletest.MyView android:layout_width="wrap_content" android:layout_height="wrap_content" myapp:backgroud="@drawable/ic_launcher" myapp:number="one" myapp:text="sdfkks" myapp:textColor="@color/black" myapp:textSize="15sp" /> </LinearLayout></span></span>
补充一些常用的format
reference 指定某资源ID
string 字符串
dimension 尺寸
color 颜色
boolean 布尔
integer 整型
float 浮点型
fraction 百分数
enum 枚举
flag 位运算
xml 中定义好自己的属性值,在在代码中得到值,或使用默认值
public CText(Context context, AttributeSet set) {
super(context, set);
this.context = context;
initView();
TypedArray array = context.obtainStyledAttributes(set,R.styleable.CEditText);
initAttrs(array);
}
private void initAttrs(TypedArray a) {
hint = a.getString(R.styleable.CEditText_cHint);
text = a.getString(R.styleable.CEditText_cText);
setAttrs();
a.recycle();
}
转载请注明出处:http://blog.csdn.net/chenlinfeng772885775/article/details/50911702
相关文章推荐
- Android TextView自定义实现@用户跳转
- android自定义view之刷新验证码
- 阅读《Android 从入门到精通》(32)——Bundle
- Android的Activity屏幕切换动画(一)-左右滑动切换
- Android 无需root实现apk的静默安装
- Android工程编译的时候报libmaliinstr.so not found的错误是为什么呢?
- Android中对Handle机制的理解
- Android如何使用API
- android的输入框EditText限制条件setFilters
- Android Java与C++的调用
- Android--Handler的使用方法:在子线程中更新界面
- android自定义控件之Dialog详解
- [置顶] android开发之当设置textview多少字后以省略号显示。限制TextView的字数
- android 二维码 helloworld (二维码的生成与扫描)
- [Android] ListView中getView的原理+如何在ListView中放置多个item
- Android开发_控制硬加速hardwareAccelerated
- 基于Android的ELF PLT/GOT符号重定向过程
- 在Android项目中,遇到过的坑
- android 图片缓存LruCache
- android五种布局模式