自定义组合控件
来源:互联网 发布:空间数据的编辑 编辑:程序博客网 时间:2024/06/07 09:43
自定义组合控件
public class SettingItemView extends RelativeLayout { //命名空间namespace private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.itheima52.mobilesafe"; private TextView tvTitle; private TextView tvDesc; private CheckBox cbStatus; private String mTitle; private String mDescOn; private String mDescOff; //有属性和样式的构造方法 public SettingItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } //只有属性的构造方法 public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); // 根据属性名称,获取属性的值 mTitle = attrs.getAttributeValue(NAMESPACE, "title"); mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on"); mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off"); initView(); // int attributeCount = attrs.getAttributeCount(); // // for (int i = 0; i < attributeCount; i++) { // String attributeName = attrs.getAttributeName(i); // String attributeValue = attrs.getAttributeValue(i); // // System.out.println(attributeName + "=" + attributeValue); // } } //没有属性和样式的构造方法 public SettingItemView(Context context) { super(context); initView(); } /** * 初始化布局 */ private void initView() { // 将自定义好的布局文件设置给当前的SettingItemView View.inflate(getContext(), R.layout.view_setting_item, this); tvTitle = (TextView) findViewById(R.id.tv_title); tvDesc = (TextView) findViewById(R.id.tv_desc); cbStatus = (CheckBox) findViewById(R.id.cb_status); setTitle(mTitle);// 设置标题 } //对外提供的在代码中动态设置控件的title public void setTitle(String title) { tvTitle.setText(title); } //对外提供的在代码中动态设置控件的desc public void setDesc(String desc) { tvDesc.setText(desc); } /** * 返回勾选状态 * * @return */ public boolean isChecked() { return cbStatus.isChecked(); } public void setChecked(boolean check) { cbStatus.setChecked(check); // 根据选择的状态,更新文本描述 if (check) { setDesc(mDescOn); } else { setDesc(mDescOff); } }}
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:itheima="http://schemas.android.com/apk/res/com.itheima52.mobilesafe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="@style/TitleStyle" android:text="设置中心" /> <com.itheima52.mobilesafe.view.SettingItemView android:id="@+id/siv_update" android:layout_width="match_parent" android:layout_height="wrap_content" itheima:desc_off="自动更新已关闭" itheima:desc_on="自动更新已开启" itheima:title="自动更新设置" /></LinearLayout>
自定义属性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="SettingItemView"> <attr name="title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable></resources>
0 0
- 自定义控件-组合控件
- 自定义组合控件,组合模式
- Android自定义组合控件
- android自定义组合控件
- 自定义组合控件
- 1.11 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件.txt
- Android自定义组合控件
- 07自定义组合控件
- 自定义组合控件
- android组合自定义控件
- android 自定义组合控件
- 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- python获取飞讯免费ss账号
- viewpager左右滑动的 +Tab指示(方式1-------------RadioGroup+ViewPage+Fragment)
- 向每一个错误致敬——用户上传破损图片时,你的App会怎样?
- jquery当鼠标移动到button按钮控件时,会出现提示信息
- HDU 5434 矩阵快速幂
- 自定义组合控件
- linux驱动之并发与竟态
- 这么多开源框架,该用哪个好
- git更新远程代码到本地
- 第五篇
- webrtc代码更新
- C++制作俄罗斯方块 日志
- 你所谓的努力,有事其实一文不值-------献给深陷bat的码农们
- 使用Qt制作一个简易计算器