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或其子类来实现. ...
随机推荐
- Spirng MVC启动流程
以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置: 修改web.xml,添加servlet定义.编写servletname-servlet.xml( servletna ...
- WPF转换器:时间转换为字符串
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureI ...
- Hadoop运行单词统计
1.创建input文件夹 hadoop fs -mkdir input 2.上传文件到hadoop hadoop fs -put /root/data/output.txt input 3.运行wor ...
- Spark算子---实战应用
Spark算子实战应用 数据集 :http://grouplens.org/datasets/movielens/ MovieLens 1M Datase 相关数据文件 : users.dat --- ...
- Extjs4 页面加载先白屏后显示的bug解决
通过Extjs MVC结构做好页面后,加载过程中发现,会瞬间白屏,然后呈现extjs界面的问题,当类似页面放置到iframe中时,会显得非常怪异. 可通过下图体验下. 当我单击“意见反馈”菜单,在右侧 ...
- 再谈git的http服务
因为git服务器搬迁,需要重新安装git服务器,在网上搜索了下,发现之前的方法太复杂,复杂到自己都没彻底弄明白.其实通过git自带的git-http-backend脚本配合apache2的http服务 ...
- 彻底清除Github上某个文件以及历史
注意:如下操作会删除选中的文件以及历史记录,若你想保留最新版本的记录,请做好备份. cd进入到你的本地项目文件夹,然后依次执行下面6行命令即可: git filter-branch --force - ...
- Qt入门——使用QT+VS2008开发windows应用程序
1.文件->新建->项目 Qt4 Projects 右边已安装模板当中选择At Application. 确定 2.选择需要使用的QT库 下一步 3. “class name”:指定类的名 ...
- CRNA的使用
为了能让更多人感受 RN 的魅力,早在几个月前 React-community 就给出了一个不错的解决方案,可以让一个完全没接触过 RN 的人,从配环境开始5分钟实现 Hello Wolrd 的编写( ...
- 4、QT分析之调试跟踪系统
原文地址:http://blog.163.com/net_worm/blog/static/127702419201002004518944/ 在我们前面的分析中,经常看到qWarning()和qDe ...