自定义组合控件
来源:互联网 发布:淘宝运营推广的方式 编辑:程序博客网 时间:2024/04/30 03:34
在Android开发中经常遇到需要自定义一些组件来实现自己想要的效果,比如下面的这个效果
这个在设置中最常见,当然也可以用一个RelativeLayout来实现,不过当内容过多的时候使用自定义组合控件是最好的选择。
代码如下:
ui_setting_view.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="68dp" android:orientation="vertical" > <TextView android:id="@+id/tv_setting_view__title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置标题" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tv_setting_view__content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/tv_setting_view__title" android:layout_below="@id/tv_setting_view__title" android:text="设置内容" android:textAppearance="?android:attr/textAppearanceSmall" /> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:clickable="false" android:focusable="false" android:gravity="center_vertical" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@drawable/listview_devider" /></RelativeLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="setting_view_style"> <attr name="title" format="string"></attr> <attr name="content" format="string"></attr> <attr name="check_text" format="string"></attr> <attr name="uncheck_text" format="string"></attr> </declare-styleable></resources>
SettingView.java
public class SettingView extends RelativeLayout {private View view;private TextView tvTitle, tvContent;private CheckBox cbStaus;private String check_text, uncheck_text;public SettingView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);view = View.inflate(context, R.layout.ui_setting_view, this);}public SettingView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);// 把属性集和我们自己定义的属性集合建立映射关系TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.setting_view_style);String content = typedArray.getString(R.styleable.setting_view_style_content);String title = typedArray.getString(R.styleable.setting_view_style_title);check_text = typedArray.getString(R.styleable.setting_view_style_check_text);uncheck_text = typedArray.getString(R.styleable.setting_view_style_uncheck_text);tvTitle.setText(title);tvContent.setText(uncheck_text);typedArray.recycle();}private void initView(Context context) {view = View.inflate(context, R.layout.ui_setting_view, this);tvTitle = (TextView) findViewById(R.id.tv_setting_view__title);tvContent = (TextView) findViewById(R.id.tv_setting_view__content);cbStaus = (CheckBox) findViewById(R.id.cb);}public SettingView(Context context) {super(context);// 创建两个TextView 一个checkboxview = View.inflate(context, R.layout.ui_setting_view, this);}/** * * @return 自定义控件checkBox的选中状态 */public boolean isChecked() {return cbStaus.isChecked();}/** * * @param checked * cb状态 */public void setChecked(boolean checked) {cbStaus.setChecked(checked);if (checked) {setContext(check_text);}else {setContext(uncheck_text);}}/** * 设置内容 * * @param text */public void setContext(String text) {tvContent.setText(text);}}
activity_setting.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:zhong="http://schemas.android.com/apk/res/com.zhong.mobilesafe" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".SettingActivity" > <TextView style="@style/text_title_style" android:text="设置中心" /> <com.zhong.mobilesafe.ui.SettingView android:id="@+id/sv_setting_update" android:layout_width="match_parent" android:layout_height="wrap_content" zhong:check_text="自动更新已开启" zhong:content="自动更新没有开启" zhong:title="设置自动更新" zhong:uncheck_text="自动更未已开启" > </com.zhong.mobilesafe.ui.SettingView>
</LinearLayout>
在这里如果要重复使用这个自定义的组件只需要复制一下代码就可以了
<com.zhong.mobilesafe.ui.SettingView android:id="@+id/sv_setting_update" android:layout_width="match_parent" android:layout_height="wrap_content" zhong:check_text="自动更新已开启" zhong:content="自动更新没有开启" zhong:title="设置自动更新" zhong:uncheck_text="自动更未已开启" > </com.zhong.mobilesafe.ui.SettingView>
总结以上自定义组件的步骤:
1、写一个类继承ViewGroup
2、重写构造方法
3、直接在xml或者代码里面就可以使用这个自定义的view对象
4、添加自定义的属性,values目录attrs文件创建declare-styleable自定义属性
5、在R文件中自动生成我们自己定义的属性
6、申明命名空间 xmlns:<自定义tag>="http://schemas.android.com/apk/res/程序包名"
7、tag:attr=""
8、在代码的构造方法里面读取自定义的配置
//把属性集和我们自己定义的属性集合简历映射关系
9、通过代码设置读取到的信息
0 0
- 自定义控件-组合控件
- 自定义组合控件,组合模式
- Android自定义组合控件
- android自定义组合控件
- 自定义组合控件
- 1.11 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件
- 自定义组合控件.txt
- Android自定义组合控件
- 07自定义组合控件
- 自定义组合控件
- android组合自定义控件
- android 自定义组合控件
- 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- 黑马程序员-OC中对象方法的声明与调用(多个参数)
- 游戏开发九宫格战斗架构解析详解
- oralce创建job
- 如何在泰国#组联系人列表中的联系人姓名分类?
- 退出返回值 - sh
- 自定义组合控件
- ehcache 使用及与Spring整合
- c++ 中 指针的指针** 与 指针的引用 *&
- 展开与收起效果(文章效果)
- mysql_query("set names gb2312")设置客户端字符集
- Windows8下Visual Studio 2003全文搜索未响应解决办法
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (
- 医院不会告诉你的秘密:如何预知宝宝性别!
- 在tomcat中配置https的技巧