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或其子类来实现. ...
随机推荐
- Mac OS下 Redis2.6.14部署记录
Mac OS下 Redis2.6.14部署记录 部署一个Redis作为缓存进行验证,记录部署过程. 官网:http://redis.io/,目前最近稳定版为2.6.14 解压,进入目录.按照READ ...
- 解决mysql开启GTID主从同步出现1236错误问题
解决mysql开启GTID主从同步出现1236错误问题 最近遇到mysql开启gtid做复制时,从库出现1236错误,导致同步无法进行,本文就这问题记录下处理步骤,有关gtid知识在这里不做介 ...
- Android安装后没有完成和打开按钮
File apkFile = new File(filePath); Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVIT ...
- 扫描二维码或其他操作情况下返回界面,onActivityResult()不执行的问题
在使用第三方zxing扫描时,部分手机(好像都是4.4及以下版本的手机)扫描后不调用onActivityResult()返回结果. 调试发现zxing的扫描界面CaptureActivity 在注册时 ...
- javascript完美实现图片拖动改变顺序
在web页面中,需要改变多个元素的位置,可以通过元素拖动来实现.HTML5中加入了一个全局属性draggable,通过设置true/false来控制元素是否可拖动. 下面以图片拖动为例,用jQuery ...
- Python __str__函数
class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s" ...
- How MapReduce Works
转自:http://blog.csdn.net/luyee2010/article/details/8624470 一.从Map到Reduce MapReduce其实是分治算法的一种实现,其处理过程亦 ...
- IDEA 在某个工程下一个module如何使用另一个module中的资源文件(.xml .prop等)
问题如题,经google,解决方案有四种,选择了比较直观有效的一种罗列如下: 因为项目采用maven管理,所以我们可以在module2下的pom.xml制定<resources>的路径,让 ...
- JDBC删除数据库实例
在本教程将演示如何在JDBC应用程序中删除一个指定的数据库. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模式中创建数据库. 要执行以下示例,需要用实际用户名和密码 ...
- perl学习(二)正则表达式
模式分组: /fred+/会匹配freddd这样的 /(fred)+/会匹配fredfredfred这样的 /(fred)*/则会匹配abcdef任意的 圆括号同时也使得重新使用某些字符串成为可能.反 ...