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、完成

正常显示

时间: 2024-12-09 00:34:11

Android开发学习笔记-自定义组合控件的相关文章

Android开发学习笔记-自定义组合控件的过程

自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需求,定义一些API方法: ----------------------------------4.根据需要,自定义控件的属性,可以参照TextView属性: 5.自定义命名空间,例如: xmlns:itheima="http://schemas.android.com/apk/res/<包名&

【转】android UI进阶之自定义组合控件

[源地址]http://blog.csdn.net/notice520/article/details/6667827 好久没写博客了.实在是忙不过来,不过再不总结总结真的不行了.慢慢来吧,有好多需要去总结的,博客里还是记录ui方面的. 今天和大家分享下组合控件的使用.很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法.今天就来介绍下如何使用组合控件,将通过两个实例来介

Android开发学习笔记-自定义TextView属性模版

如果项目中有很多个控件使用的是同一种样式,则为了方便,可以将样式设置到系统中去,这样使用的时候会方便很多. 下面是自定义样式模版的方法. 1.在style.xml文件中添加自己要设置的样式内容 <resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devic

Android开发学习笔记-自定义对话框

系统默认的对话框只能显示简单的标题内容以及按钮,而如果想要多现实其他内容则就需要自定义对话框,下面是自定义对话框的方法. 1.先定义对话框的模版 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300d

【Android开发学习笔记】【第四课】基础控件的学习

通过一个简单的例子来学习下面几种控件: 1.TextView:简单的文本显示控件 2.EditText:可以编辑的文本框 3.Button:按钮 4.Menu:这里指的是系统的Menu 5.Toast:消息提示控件,类似于MFc的tip(不知道理解的对不对) 顺便用到上一次学习的多个Activity之间传递数据的技术,来做一个小的计算乘法的case 步骤: (1)主Activity 和显示结果的 Activity 都采用线性布局,下面是布局文件的源代码: <LinearLayout xmlns:

【android】自定义组合控件PullToRefreshRecyeclerView

场景:自从Android 5.0发布以来,越来越多的开发者开始接触RecyeclerView,但是RecyclerView如何实现下拉刷新,上拉加在更多.于是我就偷懒 写了一个,以供大家参考和学习,以待大家改进. 构思:想必大家对SwipeRefreshLayout这个控件有一定了解,没错本次自定义组合控件也就是SwipeRefreshLayout与RecyeclerView的组合. 那么我们一步一步来实现: 1.首先写一个组合布局如下:pulltorefreshrecyclerview.xml

Android自定义控件之自定义组合控件(三)

前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本. 使用自定义组合控件的好处? 我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现标题栏来对比自定义组件带来的好处,毕竟好的东西还是以提高开发效率,降低开发成本为导向的. 1.)第一种方式:直接在每个xml布局中写相同的标题栏布局代码 <?xm

android 自定义组合控件

自定义控件是一些android程序员感觉很难攻破的难点,起码对我来说是这样的,但是我们可以在网上找一些好的博客关于自定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲下自定义组合控件,这个特别适合在标题栏或者设置界面,看下面图: 就非常适合使用组合控件了,现在写一个玩玩: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android 自定义组合控件小结

引言 接触Android UI开发的这段时间以来,对自定义组合控件有了一定的了解,为此小结一下,本文小结内容主要讨论的是如何使用Android SDK提供的布局和控件组成一个功能完整组合控件并将其封装为面向对象的类,而并非讨论如何继承自SDK提供的控件类(比如TextView),对其进行自定义扩展的问题. 进入正题前,我们先来看一组功能需求 假设在手机需求上,那么如上三个界面我们可以使用三个Activity,每个Activity一个布局文件,实现起来比较独立,但是假设在Android pad上要