在android studio 中自定义属性的使用
2016-05-13 12:55
429 查看
第一步:
首先在自己项目中res/values目录下创建attrs.xml文件,在此文件中写自己自定义的属性,具体形式如下代码
< declare-styleable name=”SettingView”>在此标签中,name的值要写成自定义view类的类名SettingView
在< attr name=”safe_setting_title” format=”color”/>标签中,name为要设置的属性名,format为要设置属性的类型,其类型有string、integer、dimension、reference、color、enum。
第二步
在布局文件中使用上述属性
要注意的有两条
1.在androidstudio中引用自定义的属性,要加一条命名空间,命名空间的写法如下xmlns:myname=”http://schemas.android.com/apk/res-auto”
myname是自己随便命名的,其后面引号中的内容是固定的,也就是在androidstudio中所有的自定义属性引用,在此处都是这样写,这点不同于eclipse。
2.
如这两行写的,之前写属性时经常写android:layout_width,现在android:要换成myname:这样写完,引用属性基本完成。
第三步
在自定义的组合控件的构造方法中获取设置的属性
通过这个构造方法可以获取设置的自定义属性
OK!完成以上步骤算是完成对自定义属性的引用,在此标记
首先在自己项目中res/values目录下创建attrs.xml文件,在此文件中写自己自定义的属性,具体形式如下代码
"><resources> <declare-styleable name="SettingView"> <attr name="safe_setting_title" format="string"/> <attr name="des_on" format="string"/> <attr name="des_off" format="string"/> /> </declare-styleable> </resources>
< declare-styleable name=”SettingView”>在此标签中,name的值要写成自定义view类的类名SettingView
在< attr name=”safe_setting_title” format=”color”/>标签中,name为要设置的属性名,format为要设置属性的类型,其类型有string、integer、dimension、reference、color、enum。
第二步
在布局文件中使用上述属性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myname="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.admin.phonesafe.SettingActivity" > <com.example.admin.phonesafe.view.SettingView android:id="@+id/settingview_" 4000 android:layout_width="match_parent" android:layout_height="wrap_content" myname:safe_setting_title="手机安全" myname:des_on="zhangsan"> </com.example.admin.phonesafe.view.SettingView> </LinearLayout>
要注意的有两条
1.在androidstudio中引用自定义的属性,要加一条命名空间,命名空间的写法如下xmlns:myname=”http://schemas.android.com/apk/res-auto”
myname是自己随便命名的,其后面引号中的内容是固定的,也就是在androidstudio中所有的自定义属性引用,在此处都是这样写,这点不同于eclipse。
2.
如这两行写的,之前写属性时经常写android:layout_width,现在android:要换成myname:这样写完,引用属性基本完成。
第三步
在自定义的组合控件的构造方法中获取设置的属性
import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.util.AttributeSet; import android.view.View; import android.widget.CheckBox; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.myname .phonesafe.R; import com.example.myname .phonesafe.utils.LogUtil; /** * Created by myname on 2016/5/12. */ public class SettingView extends RelativeLayout { private static final String TAG = "SettingView"; private View rootView;//组合自定义控件对象的根节点 private TextView mTitleTv; private TextView mDesTv; private CheckBox mCheckBox; /** * 自定义方法操作组合控件的子控件 * 吧自定义组合控件的xml文件示例化为对象,并且添加到当前对象中作为当前对象的子控件 */ public SettingView(Context context) { super(context); LogUtil.i(TAG, "SettingView,context"); init(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public SettingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); LogUtil.i(TAG, "SettingView,context, attrs, defStyleAttr, defStyleRes"); init(); } public SettingView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); LogUtil.i(TAG, "SettingView,context, attrs, defStyleAttr"); init(); } public SettingView(Context context, AttributeSet attrs) { super(context, attrs); LogUtil.i(TAG, "SettingView,context, attrs"); init(); //取得自定义属性 int count=attrs.getAttributeCount(); for (int i=0;i<count;i++){ LogUtil.i(TAG,attrs.getAttributeValue(i)); } } //初始化自定义组合控件 private void init() { rootView = View.inflate(getContext(), R.layout.setting_view, this); mTitleTv = (TextView)rootView.findViewById(R.id.tv_setting_item_title); mDesTv = (TextView) rootView.findViewById(R.id.tv_setting_item_des); mCheckBox = (CheckBox) rootView.findViewById(R.id.checkBox); } //自定义方法 //设置标题 public void setTitle(String title) { mTitleTv.setText(title); } //设置描述 public void setDes(String des) { mDesTv.setText(des); } //设置复选项 public void setChecked(boolean isChecked) { mCheckBox.setChecked(isChecked); } //取得组合控件的复选状态 public boolean getChecked(){ return mCheckBox.isChecked(); } }
通过这个构造方法可以获取设置的自定义属性
OK!完成以上步骤算是完成对自定义属性的引用,在此标记
相关文章推荐
- Androidstudio的manifest里面meta-data填写key的问题
- android开发步步为营之103:android分享音频视频图片和文字功能的实现
- Android 开发环境搭建及常见问题解决
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- Android之Proguard语法
- Android多线程下载断点续传
- android 中从数据库读取短信验证码
- RecyclerView添加头脚布局,使用SwipeRefreshLayout的实现
- AsyncTask的缺陷
- Android Fragment release 有时黑屏解决办法
- Android单点触控技术,对图片进行平移,缩放,旋转操作
- Android播放系统声音源码
- android集成友盟数据收集环境及简单实用
- Android开发之 EditText 上的清空按钮
- Android studio的项目名称变成绿色的解决方法
- Android时间选择器
- Android 安全综述
- Android 错误提示:Conversion to Dalvik format failed with error 1 解决办法
- android获取gallery中的图片以及调用系统相机
- 终结Android项目的R文件报错问题