自定义组合控件:textView+checkBox
来源:互联网 发布:js中邮箱的正则表达式 编辑:程序博客网 时间:2024/06/05 15:18
自定义组合控件:textView+checkBox
先实现布局文件ui_setting_view.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/rl_set_update"android:layout_width="match_parent"android:layout_height="60dp"android:layout_gravity="center_vertical"android:background="@drawable/background" ><TextViewandroid:id="@+id/tv_ui_setting"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="20dp"android:text="开启自动更新"android:textSize="25sp" /><CheckBoxandroid:id="@+id/cb_set_update"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="20dp"android:clickable="false"android:focusable="false" /><View //一条线android:layout_width="match_parent"android:layout_height="0.1dp"android:layout_marginTop="1dp"android:layout_marginRight="10dp"android:layout_marginLeft="10dp"android:layout_below="@id/tv_ui_setting"android:layout_margin="2dp"android:background="#88000000" /></RelativeLayout>
定义一个类继承LinearLayout ,实现父类的构造函数,初始化控件,加上一个判断是否勾选的函数,还有一个设置Checkbox勾选的函数
public class SettingCheckView extends LinearLayout {
private CheckBox set_update;
public SettingCheckView(Context context, AttributeSet attrs) {
super(context, attrs);
initial(context);
//获取属性定义的文本
String bigtitle=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.cca.mobilephone", "bigtitle");
TextView tv_title=(TextView) findViewById(R.id.tv_ui_setting);
//设置文本
tv_title.setText(bigtitle);
}
public SettingCheckView(Context context) {
super(context);
initial(context);
}
private void initial(Context context) {
this.setOrientation(LinearLayout.VERTICAL);
this.addView(View.inflate(context, R.layout.ui_setting_view, null));
set_update=(CheckBox) findViewById(R.id.cb_set_update);
}
/**
* 判断checkbox是否被勾选
*/
public boolean isChecked(){
return set_update.isChecked();
}
/**
* 设置checkbox是否被勾选
*/
public void setChecked(boolean checked){
set_update.setChecked(checked);
}
} 这样一个自定义控件带CheckBox的TextView就定义好了,不过要想正真使用它还需要注意以下内容:
在使用它的布局文件中加入
1、这个控件是没有设置文本的功能的,所以我们要自定义一个设置文本的属性:在使用自定义控件的当前布局文件中先声明属性的命名空间
xmlns:mobilephone="http://schemas.android.com/apk/res/工程的包名"
2、在values定义一个属性文件attrs.xml文件,在里面声明功能
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="SettingCheckView"><attr name="bigtitle" format="string"/></declare-styleable>//或者<declare-styleable name="SettingCheckView"><attr name="title" format="string"/></declare-styleable></resources>
3、可以使用自定义控件了
<com.cca.mobilephone.ui.SettingCheckView
android:layout_width="match_parent"android:layout_height="wrap_content"
mobilephone:bigtitle="我是功能2"
/>
做到这里就可以使用自定义组合控件了,功能可以设置文本内容,想增加其他的属性,在attrs中定义出来就可以使用了。要是想在另外的布局中使用,只要重新加入命名空间就可以使用了。
0 0
- 自定义组合控件:textView+checkBox
- 自定义组合控件textview
- 自定义控件----继承framLayout --textview与checkBox
- 自定义控件(17)---布局方式组合系统控件,并封装自定义控件思想(自定义CheckBox)
- Android自定义组合控件之实现CheckBox变化
- 自定义控件-组合控件
- 自定义控件,checkbox
- 自定义控件之CheckBox
- [自定义控件学习]Textview
- 自定义TextView控件
- 自定义旋转TextView控件
- 自定义控件textview
- 自定义倒计时TextView控件
- 自定义控件1---TextView
- 自定义控件3---TextView
- Android自定义TextView控件
- 自定义控件动画TextView
- [安卓自定义控件]自定义组合控件——支持根据TextView行数展开折叠的引文显示控件ExpandableReferenceView
- Kafka设计解析(二)- Kafka High Availability (上)
- mac osx 10.11.6 安装eclipse
- 1042. Shuffling Machine (20)-PAT甲级真题
- ZeroMQ进阶
- 找不到更好的?为什么不试一试呢?
- 自定义组合控件:textView+checkBox
- linux账户管理
- C++ :类中的静态私有数据成员
- 二分_A
- Unity Mecanim动画的实现(一):基本程序
- hls流媒体:ts流格式介绍
- Linux中的几个问题
- android属性之noHistory
- Redis学习03-对String类型的操作