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或其子类来实现. ...
随机推荐
- vagrant中css,img不生效的问题
用vagrant搭建了一个共享开发平台,修改css文件后,页面看不到效果,通过查看源文件地址,发现新修改的Css文件并没有加载进来,加载的还是旧的文件地址.一开始以为是浏览器有缓存,清空了各种浏览器缓 ...
- java.io.BufferedOutputStream 源码分析
BufferedOutputStream 是一个带缓冲区的输出流,通过设置这种输出流,应用程序就可以字节写入到缓冲区中,当缓冲区满了以后再调用底层系统,而不必针对每次字节写入调用底层系统,从而提高系 ...
- mac上怎么安装dmg
双击dmg文件,就会打开了,里面一般就是应用程序,拖到Finder-应用程序(如果里面是pkg格式,就是安装包,双击安装),然后还要注意一个安装完了以后,要把刚才载入的dmg推出,方法是把桌面上那个图 ...
- Parse how to write flash in uefi shell.
Step: 1. Enable 2. Read 3. Write 4. Disable FI_GUID gEfiSFlashProtocolGuid = FLASH_P ...
- C# 不借助第三个变量实现两整数交换
c#中实现两个int变量的数值互换,在借助第三个变量做临时载体的情况下,非常简单. ; ; int c ; c = a; a = b; b = c; 如果不借助第三方变量,有几种实现的方法: 1.异或 ...
- IIS7.5 中启用rest服务,Delete、Put
WebDAV 是超文本传输协议 (HTTP) 的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...
- Excel相同内容如何设置相同的背景色
有这样一个需求就是实现EXCEL的相同内容的背景色相同.并且内容不同的时候达到隔行变色的效果,记录下实现的效果,如果大家有什么更好的办法请给我指点一下.具体操作如下: 首先将是比较的列"20 ...
- Debug 单步执行命令step into/step out/step over的区别
总结一下在debug中三种调试的区别: step into就是单步执行,遇到子函数就进入并且继续单步执行: step over是在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数 ...
- Ogre 渲染目标解析与多文本合并渲染
实现目标 因为需求,想找一个在Ogre中好用的文本显示,经过查找和一些比对.有三种方案 一利用Overlay的2D显示来达到效果. http://www.ogre3d.org/tikiwiki/tik ...
- python-切片实例
针对list或tuple取指定范围的操作.可以使用切片(slice),非常有用 1.list:可变数组 L=['a','b','c','d','e'] >>> L[0:3] #从第0 ...