Android 手机卫士--自定义组合控件构件布局结构

由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现

本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址。

自定义组合控件

1.将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用此布局结构的时候,直接使用组合控件对应的对象.

2.将组合控件的布局,抽取到单独的一个xml中

新建布局文件:setting_item_view.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="wrap_content" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动更新设置"
            android:textColor="#000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_des"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:text="自动更新已关闭"
            android:textColor="#000"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/cb_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/tv_des"
            android:background="#000" />
    </RelativeLayout>

</RelativeLayout>

3.通过一个单独的类SettingItemView.java,去加载此段布局文件.

package com.wuyudong.mobilesafe.view;

import com.wuyudong.mobilesafe.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SettingItemView extends RelativeLayout {

    private TextView tv_des;
    private CheckBox cb_box;

    public SettingItemView(Context context) {
        this(context, null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, null, 0);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // xml-->view 将设置界面的条目转换成view对象
        View.inflate(context, R.layout.setting_item_view, this);
        // 等同于以下两行代码
        /*
         * View view = View.inflate(context, R.layout.setting_item_view, null);
         * this.addView(view);
         */

        //自定义组合控件中的标题描述
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_des = (TextView) findViewById(R.id.tv_des);
        cb_box = (CheckBox) findViewById(R.id.cb_box);
    }

}

这样只需要简单的几行代码就可以完成布局文件的调用

<?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
        style="@style/TitleStyle"
        android:text="设置中心" />

    <!--
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动更新设置"
            android:textColor="#000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_des"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:text="自动更新已关闭"
            android:textColor="#000"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/cb_box"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <View
            android:layout_below="@id/tv_des"
            android:background="#000"
            android:layout_width="match_parent"
            android:layout_height="1dp"

            />
    </RelativeLayout>
    -->

    <com.wuyudong.mobilesafe.view.SettingItemView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.wuyudong.mobilesafe.view.SettingItemView>

</LinearLayout>

运行项目后,有如下效果:

时间: 2024-08-10 07:19:53

Android 手机卫士--自定义组合控件构件布局结构的相关文章

[android] 手机卫士自定义组合控件

设置中心 新建SettingActivity 设置GridView条目的点击事件 调用GridView对象的setOnItemClickListenner()方法,参数:OnItemClickListenner对象 匿名内部类实现,重写onItemClick()方法,传递进来的参数: parent是GridView对象,view是当前View对象,position是当前索引 switch判断,当时设置中心的索引时,跳转到设置中心 设置中心界面 使用相对布局,右边的<CheckBox/> 位于父

[android] 手机卫士自定义滚动控件

TextView控件设置单行显示 android:singleLine=”true” 设置TextView开始的位置显示省略号,android:ellipsize=”start” 设置滚动属性,android:ellipsize=”marquee” 当控件有焦点的时候,才会调用那个滚动效果 新建一个ui包 新建一个类FocusedTextView继承系统的TextView 重写构造方法 重写isFoused()方法,返回true,就可以让它生来就有焦点,其实并没有焦点,只是欺骗了android系

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

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

android自定义控件(五) 自定义组合控件

转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 通过代码或者通过xml设置自定义控件的属性 1.控件布局:以Linearlayout为根布局,一个TextView,一个ImageButton.  Xml代码 [html] view plaincopy < ?xml version="1.0" 

Android 自定义组合控件小结

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

Android自定义控件——自定义组合控件

转载请注明出处http://blog.csdn.net/allen315410/article/details/39581055  前面几篇博文介绍了Android如何自定义控件,其实就是讲一下如何"从无到有"的自定义一个全新的控件,继承View或者继承ViewGroup,复写其相关方法,这种自定义控件的方式相对来说难度较大,而且并不是所有需要新控件的情况下,都要这样进行.有很多情况下,我们只要运用好Android给我提供好的控件,经过布局巧妙的结合在一起,就是一个新的控件,我称之为&

自定义组合控件和在自定义控件中使用自定义属性

今天,整理了一下我平时的笔记,写一个比较简单的自定义组合控件,仅供小白参考,大神请绕道,希望能够对大家有一些帮助 首先,得明白为什么我们需要自定义组合控件,它是因为原有控件并不能满足开发的需求,或者说并不能达到我们想要的一种效果,这个时候,就需要我们自己定义一些控件,以达到目的 ![先来看一下效果](http://img.blog.csdn.net/20160716224219109) 个人总结自定义控件的步骤: 1.先写一个布局,这里我用的是一个相对布局,我这里的相对布局就是根布局了 <?xm

Android实例-手机安全卫士(七)-自定义组合控件

一.目标. 将多个系统控件(TextView.Button.CheckBox等)组合成一个自定义的控件,并像系统控件一样使用.如图所示第1个自动更新控件是根据相对布局放置而成的,第2个自动更新控件即为自定义组合控件,它可以想一般的TextView等系统控件一样重复使用. 自定义控件如图: 二.代码实现. 1.在layout文件夹下新建一个xml文件(取名model_setting_item.xml),用于保存自定义控件的布局. 2.在新建的xml文件(model_setting_item.xml

Android自定义组合控件--底部多按钮切换

效果图: 现在市场上大多数软件都是类似于上面的结构,底部有几个按钮用于切换到不同的界面.基于OOP思想,我想把下面的一整块布局封装成一个类,也就是我们的自定义组合控件-底部多按钮切换布局,我把它叫做BottomLayout 看上面的布局,几个按钮横向排列,我们先看一下布局 最外面LinearLayout 方向 horizontal,然后5个weight相同的RelativeLayout,每个RelativeLayout里面有一个Button(用了显示选中状态)个ImageView(用来显示红点)