学习lib_common 公共业务模块中知识
declare-styleable:自定义控件的属性
有时候官方给的控件不够用,我们需要自己定义控件,
① 在res/values文件下定义attrs.xml文件来定义我们需要的控件;
*<?xml version="1.0" encoding="utf-8"?>*
*<resources>*
*<declare-styleable name="ToolBar">*
*<attr name="buttonNum" format="integer"/>*
*<attr name="itemBackground" format="reference|color"/>*
*</declare-styleable>*
*</resources>*
需要用包围所有属性。
其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到"R.styleable.ToolBar_buttonNum",很显然,他在每个属性前面都加了"ToolBar_"。
然后是format,就是这个属性集的格式,在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum…如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。
② 在使用时在布局文件中使用上面定义的属性
*<?xml version="1.0" encoding="utf-8"?>*
*<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"*
*xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar"*
*android:orientation="vertical"*
*android:layout_width="fill_parent"*
*android:layout_height="fill_parent"*
*>*
*<cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar"*
*android:layout_width="fill_parent"*
*android:layout_height="wrap_content"*
*android:layout_alignParentBottom="true"*
*android:background="@drawable/control_bar"*
*android:gravity="center"*
*toolbar:buttonNum="5"*
*toolbar:itemBackground="@drawable/control_bar_item_bg"/>*
*</RelativeLayout>*
③ 在kt中使用时引入
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);
buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5);
itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);
a.recycle() //释放资源
在自定义组件中获取属性集的隐痛,这里是固定格式,首先是获取这个TollBar,兵命名为a,然后获取这个styleable中的属性集,通过a.getxx获取。xx是这个属性集的format,
reference:参考指定Theme中资源ID。
1.定义:
<declare-styleable name="My">
<attr name="label" format="reference" >
</declare-styleable>
2.使用:
<Buttonzkx:label="@string/label" >
MyToolBar 公用的标题栏
好多的activity中都会使用到返回的标头,所以我们将其设置为公用的。
布局上 首先是有一个类似于箭头的图标,使用imageview,点击这个图片就会返回,然后是这个activity对相应的title,所以有以个textview,然后 是一些其他的控件,如果好多个都需要添加上,如果某些不使用就在该不适用的页面中将其的可见性设置为不可见。
① 我们想要自定义一个view,【toolBar】可以是重复使用,创建一个view继承LinearLayout,这里就是创建一个MyToolBarLayout来继承,
② 为view写一个xml,【mytitleview】在布局文件中, 只是定义了这些控件和id,但是没有指定特定的资源来源,也就是没有直接把图片插入,没有把textview的值直接规定。
③ 在values下面attr自定义控件【MyView】,定义了public_bar的控件,并声明了title、res、img、right的属性集,
④ 在定义的view中 加载 布局文件中的组件,并获取自定义控件的属性集,并对布局的控件设置资源。
//初始化自定义组件
private void initview(Context context, AttributeSet attrs) {
//加载布局文件
View view = LayoutInflater.from(context).inflate(R.layout.mytitleview, null);
//初始化布局的控件
textView_left=view.findViewById(R.id.mytitleview_left_text);
textView_right=view.findViewById(R.id.mytitleview_right_text);
textView_center=view.findViewById(R.id.mytitleview_center_text);
//获取属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
//资源(参数:1.资源id 2.默任值)
int left = typedArray.getResourceId(R.styleable.MyView_myleft, 0);
int right = typedArray.getResourceId(R.styleable.MyView_myright, 0);
int center = typedArray.getResourceId(R.styleable.MyView_mycenter, 0);
//判断是否为空 空会取默认值 不空就设置资源
if(left!=0){
textView_left.setText(left);
}
if(right!=0){
textView_right.setText(right);
}
if(center!=0){
textView_center.setText(center);
}
//添加
this.addView(view);
//释放资源
typedArray.recycle();
}
⑤ 在其他地方使用的就是 引入这个view就好
<view 所在的完整路径名
andoird: id =
android:layout_width =
android:layout_height =
app: Myview_myleft (MyView_mycenter等) =
/>
就是使用的使用可以对资源进行赋值 这里就是对MyView_myleft进行赋值。