• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > [android] 手机卫士自定义控件的属性,android卫士

[android] 手机卫士自定义控件的属性,android卫士

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android360卫士,android360手机卫士,android手机安全卫士,android手机卫士,android360安全卫士等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

[android] 手机卫士自定义控件的属性,android卫士


上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性

上一节组合控件SettingItemView中有三个控件,分别是TextView大标题,TextView描述,CheckBox复选框

自定义属性 tsh:title=”大标题” 和tsh:desc_on=”小标题开启”,tsh:desc_off=”小标题关闭”

添加命名空间,xmlns:tsh=”http://schemas.android.com/apk/res/包名"

在res/values/目录下创建 attrs.xml文件

添加节点 <declare-styleable name=”TextView”>

节点下添加节点<attr name=”title” format=”string”/>,添加其他两个属性的节点

 

在布局文件使用的时候,会调用带有两个参数的构造方法

在这个构造方法里面,会传递一个AttributeSet对象

调用AttributeSet对象的getAttributeValue()方法,得到属性值,参数:索引位置,不推荐

调用AttributeSet对象的getAttributeValue(namespace,name)方法,参数:命名空间,属性名

调用TextView对象的setText()方法,直接给设置进去

描述部分,在setChecked()方法里面,判断,再设置

SettingItemView.java

package com.qingguow.mobilesafe.ui;

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

import com.qingguow.mobilesafe.R;

public class SettingItemView extends RelativeLayout {
    private TextView tv_title;
    private TextView tv_desc;
    private CheckBox cb_status;
    private String desc_on;
    private String desc_off;
    /**
     * 初始化View对象
     * @param context
     */
    private void initView(Context context) {
        View.inflate(context, R.layout.setting_item_view, this);
        cb_status=(CheckBox) this.findViewById(R.id.cb_status);
        tv_desc=(TextView) this.findViewById(R.id.tv_desc);
        tv_title=(TextView) this.findViewById(R.id.tv_title);
        
    }
    /**
     * 判断是否选中
     * @return
     */
    public boolean isChecked(){
        return cb_status.isChecked();
    }
    /**
     * 设置是否选中
     * @param status
     */
    public void setChecked(boolean status){
        if(status){
            tv_desc.setText(desc_on);
        }else{
            tv_desc.setText(desc_off);
        }
        cb_status.setChecked(status);
    }
    /**
     * 设置显示文本
     * @param text
     */
    public void setDesc(String text){
        tv_desc.setText(text);
    }
    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
     //获取传递的属性 String title
=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "title"); tv_title.setText(title); desc_on=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_on"); desc_off=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_off"); } public SettingItemView(Context context) { super(context); initView(context); } }

 

activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tsh="http://schemas.android.com/apk/res/com.qingguow.mobilesafe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ccc"
        android:gravity="center"
        android:text="设置中心"
        android:textSize="20sp" />

    <com.qingguow.mobilesafe.ui.SettingItemView
        tsh:title="设置自动更新"
        tsh:desc_on="设置自动更新开启"
        tsh:desc_off="设置自动更新关闭"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/siv_item">
    </com.qingguow.mobilesafe.ui.SettingItemView>

</LinearLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TextView">
        <attr name="title" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>

</resources>

 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android菜单(动画菜单、360波纹菜单),android波纹
  • Android 手机卫士17--缓存清理,android17--
  • Android 手机卫士13--进程设置,android13--
  • Android 手机卫士10--应用管理器,android10--
  • Android 手机卫士8--删除通话记录,android8--
  • Android 手机卫士--导航界面4的业务逻辑,android卫士
  • Android 手机卫士--平移动画实现,android卫士
  • Android 手机卫士--获取联系人信息并显示与回显,android回显
  • Android 手机卫士--参照文档编写选择器,android选择器
  • Android 手机卫士--导航界面2,android卫士

相关文章

  • 2017-05-26Android网络编程(四)从源码解析Volley
  • 2017-05-26360多渠道打包,360打包
  • 2017-05-26安卓开发中内存问题分析(一)工具篇
  • 2017-05-222.4.4 Adapter基础讲解
  • 2017-05-26Android中使用ImageViewSwitcher实现图片切换轮播导航效果,
  • 2017-05-26Android群英传笔记系列二view的绘制(一),android群英
  • 2017-05-26如何处理 android 方法总数超过 65536 . the number of method references in a .dex file exceed 64k,android.dex
  • 2017-05-26MSM8909+Android5.1.1键盘驱动浅析
  • 2017-05-26viewPager+fragment如何刷新缓存fragment,viewpagerfragment
  • 2017-05-26Android Studio 2.1 Preview有那些更新内容

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • AS下NDK开发(一),as下ndk开发
    • android:获取手机号码和姓名实现通讯录
    • dspam运维记录
    • android环境下的即时通讯,android即时通讯
    • 如何通过cmdline获取panel型号的dtsi文件节点(qcom,lcd,id)
    • Android动态加载Activity原理
    • Android开发:优化ListView实践解析
    • Android之SharedPreferences数据存储,sharedpreferences
    • arcgis andriod 加载影像,arcgisandriod
    • 微信小程序监控,信小程序监控

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有