Android开发学习笔记-自定义组合控件
为了能让代码能够更多的复用,故使用组合控件。下面是我正在写的项目中用到的方法。
1、先写要组合的一些需要的控件,将其封装到一个布局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="68dip"
android:id="@+id/aaa"
>
<TextView
android:id="@+id/tv_update_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="是否升级" />
<TextView
android:layout_below="@id/tv_update_title"
android:id="@+id/tv_update_content"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="停止更新" /> <CheckBox
android:checked="false"
android:id="@+id/cb_isupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" /> </RelativeLayout>
2、自定义Java类
package com.frank.mobilesafe.ui; import com.frank.mobilesafe.R; import android.R.bool;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView; public class SettingItemView extends RelativeLayout {
private CheckBox cb_update;
private TextView tv_update_title;
private TextView tv_update_content; public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
} private void initView(Context context) {
// TODO Auto-generated method stub
View.inflate(context, R.layout.setting_item_view, this);
cb_update = (CheckBox) findViewById(R.id.cb_isupdate);
tv_update_title = (TextView) findViewById(R.id.tv_update_title);
tv_update_content = (TextView) findViewById(R.id.tv_update_content); } public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
} public SettingItemView(Context context) {
super(context);
initView(context);
} /**
* 检查是否选中
* @return
*/
public boolean isChecked() {
return cb_update.isChecked();
}
/**
* 设置组合控件的状态
* @param isChecked
*/
public void SetChecked(boolean isChecked) {
cb_update.setChecked(isChecked);
}
/**
* 设置描述信息
* @param isChecked
*/
public void SetDesc(String text) {
tv_update_content.setText(text);
}
}
3、在主界面中引用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/tv_maintitle"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#8866ff00"
android:gravity="center"
android:text="设置中心"
android:textSize="22sp" /> <com.frank.mobilesafe.ui.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
4、主界面调用
public class SettingActivity extends Activity {
private SettingItemView siv_update;
private SharedPreferences sp_update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
siv_update = (SettingItemView) findViewById(R.id.siv_update);
sp_update = getSharedPreferences("config",MODE_PRIVATE);
boolean update = sp_update.getBoolean("update", false);
if (update) {
siv_update.SetChecked(true);
siv_update.SetDesc("有新版本则更新");
}
else
{
siv_update.SetChecked(false);
siv_update.SetDesc("停止更新");
}
siv_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor editor = sp_update.edit();
// TODO Auto-generated method stub
if (siv_update.isChecked()) {
siv_update.SetChecked(false);
siv_update.SetDesc("停止更新");
editor.putBoolean("update", false);
}
else{
siv_update.SetChecked(true);
siv_update.SetDesc("有新版本则更新");
editor.putBoolean("update", true);
}
}
});
}
}
5、完成
正常显示
Android开发学习笔记-自定义组合控件的更多相关文章
- Android开发学习笔记-自定义组合控件的过程
自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- 【转】android UI进阶之自定义组合控件
[源地址]http://blog.csdn.net/notice520/article/details/6667827 好久没写博客了.实在是忙不过来,不过再不总结总结真的不行了.慢慢来吧,有好多需要 ...
- Android开发学习笔记-自定义TextView属性模版
如果项目中有很多个控件使用的是同一种样式,则为了方便,可以将样式设置到系统中去,这样使用的时候会方便很多. 下面是自定义样式模版的方法. 1.在style.xml文件中添加自己要设置的样式内容 < ...
- Android开发学习笔记-自定义对话框
系统默认的对话框只能显示简单的标题内容以及按钮,而如果想要多现实其他内容则就需要自定义对话框,下面是自定义对话框的方法. 1.先定义对话框的模版 <?xml version="1.0& ...
- Android开发之自定义组合控件
自定义组合控件的步骤1.自定义一个View,继承ViewGroup,比如RelativeLayout2.编写组合控件的布局文件,在自定义的view中加载(使用View.inflate())3.自定义属 ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android Studio自定义组合控件
在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
随机推荐
- Creating a Physical Standby Database 11g
1.Environment Item Primary database standby database Platform Redhat 5.4 Redhat 5.4 Hostname gc1 gc2 ...
- CentOS重置Mysql密码
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...
- C语言 · 选最大数
算法提高 选最大数 时间限制:1.0s 内存限制:512.0MB 输入3个整数a.b.c,(数的范围是[1,10000])输出其中最大的数.(用指针实现) 样例输入 2 5 1 样例 ...
- 分治算法--寻找第k大数
问题描述:给定线性序集中n个元素和一个整数k,1≤k≤n,要求找出这n个元素中第k大的元素,(这里给定的线性集是无序的). 其实这个问题很简单,直接对线性序列集qsort,再找出第k个即可.但是这样的 ...
- [redis] redis连接远程客户端查询数据
F:cd redis-2.8.12redis-cli.exe -h 192.168.6.107 -p 16680redis-cli.exe -h 192.168.6.107 -p 16681redis ...
- Error:(1, 0) Plugin with id 'com.android.application' not found
Error:(1, 0) Plugin with id 'com.Android.application' not found.Open File 这个错误是build.gradle造成的,我们打开文 ...
- FPGA内部动态可重置PLL讲解(二)
对于全局时钟的管理,涉及到关于亚稳态的知识,大家可以上网搜索相关资料,这里不再赘述.亚稳态最简单的理解形式是无法判断是处于高电平状态还是处于低电平状态,这样会导致整个系统不稳定,会出现逻辑上的错误. ...
- Python判断操作系统类型
代码: import platform def TestPlatform(): print ("----------Operation System--------------------- ...
- Numpy的array数组和标量之间的运算
矢量化 数组很重要,因为它使你不用编写循环即可对数据执行批量运算.这通常就叫做矢量化(vectorzation) 数组与数组的运算 数组与标量的算术运算
- 模式识别之检索---Bag of visual word(词袋模型)
visual words 视觉单词 http://blog.csdn.net/v_july_v/article/details/8203674 http://blog.csdn.net/pi9nc/a ...