• 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 > 手机安全卫士——在设置中心 自定义view和自定义属性,安全卫士view

手机安全卫士——在设置中心 自定义view和自定义属性,安全卫士view

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

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

手机安全卫士——在设置中心 自定义view和自定义属性,安全卫士view


自定义组合控件
1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView
2. 编写组合控件的布局文件,在自定义的View中加载
            // 将自定义好的布局文件设置给当前的SettingItemView
        View.inflate(getContext(), R.layout.view_setting_item, this);
3. 自定义属性

      删除代码中对文本的动态设置,改为在布局文件中设置

      在布局文件中增加新的命名空间

      创建attrs.xml,定义相关属性

      读取自定义的值,更新相关内容

 

activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mobilesafe="http://schemas.android.com/apk/res/com.mxn.mobilesafe"//自定义命名空间。。。在布局文件中增加新的命名空间
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        
        style="@style/TitleStyle"
        android:text="设置中心" />

    <com.mxn.mobilesafe.view.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

//自定义属性,不用android默认的属性
//从命名空间中找
mobilesafe:title
="自动更新设置" mobilesafe:desc_on="自动更新已开启" mobilesafe:desc_off="自动更新已关闭" > </com.mxn.mobilesafe.view.SettingItemView> <com.mxn.mobilesafe.view.SettingItemView android:id="@+id/siv_address" android:layout_width="match_parent" android:layout_height="wrap_content" mobilesafe:title="归属地显示设置" mobilesafe:desc_on="归属地显示已开启" mobilesafe:desc_off="归属地显示已关闭" > </com.mxn.mobilesafe.view.SettingItemView> <com.mxn.mobilesafe.view.SettingClickView android:id="@+id/scv_address_style" android:layout_width="match_parent" android:layout_height="wrap_content" > </com.mxn.mobilesafe.view.SettingClickView> <com.mxn.mobilesafe.view.SettingItemView android:id="@+id/siv_watchdog" android:layout_width="match_parent" android:layout_height="wrap_content" mobilesafe:title="看门狗设置" mobilesafe:desc_on="看门狗已开启" mobilesafe:desc_off="看门狗已关闭" > </com.mxn.mobilesafe.view.SettingItemView> </LinearLayout>
自定义属性 : attrs.xml。。创建attrs.xml,定义相关属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SettingItemView">
        <attr name="title"  format="string"></attr>
        <attr name="desc_on"  format="string"></attr>
        <attr name="desc_off"  format="string"></attr>
                
    </declare-styleable>
    
</resources>
 
SettingItemView.java
/*
 * 设置中心的自定义控件,自定义View
 */
public class SettingItemView extends RelativeLayout {
    TextView tvTitle;
    TextView tvDesc;
    CheckBox cbStatus;
    private String mtitle;
    private String mdescon;
    private String mdescoff;
String namespace
= "http://schemas.android.com/apk/res/com.mxn.mobilesafe";//命名空间 public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); // TODO Auto-generated constructor stub initView(); } public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub initView(); // int attributeCount = attrs.getAttributeCount(); // for(int i=0;i<attributeCount;i++){ // String attrsname = attrs.getAttributeName(i); // String attrvalue = attrs.getAttributeValue(i); // System.out.println(attrsname+"="+attrvalue); // // } } public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub
// 读取自定义的值,更新相关内容
//根据属性名称获取属性的值 mtitle = attrs.getAttributeValue(namespace , "title"); mdescon = attrs.getAttributeValue(namespace , "desc_on"); mdescoff = attrs.getAttributeValue(namespace , "
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

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

  • 手机安全卫士——缓存清理,安全卫士缓存清理
  • 手机安全卫士——在设置中心 自定义view和自定义属性,安全卫士view
  • Android项目:手机安全卫士(16)—— 复杂 ListView浅析

相关文章

  • 2017-05-26android布局--Android fill_parent、wrap_content和match_parent的区别,wrapparent
  • 2017-05-26Android实现按钮点击效果(第一次点击变色,第二次恢复),android按钮
  • 2017-05-26一起来学习Android自定义控件
  • 2017-05-26oracle 代码报错大全分析
  • 2017-10-15Android源码基础解析之Activity布局绘制流程
  • 2017-05-26Android中View的事件分发机制——Android开发艺术探索笔记
  • 2017-05-26Android studio Error occurred during initialization of VM 问题解决,initializationofvm
  • 2017-05-26OpenDigg安卓开源项目月报201704,opendigg安卓201704
  • 2017-05-26Android 关于“NetworkOnMainThreadException”,networkonmainthread
  • 2017-05-26android 所有焦点问题--Focus,android--focus

文章分类

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

最近更新的内容

    • 点击文本改变改行背景色,弹出对话框,改行背景
    • ListView和Adapter的配合使用以及Adapter的重写,listviewadapter
    • WEB服务器、应用程序服务器、HTTP服务器区别
    • 设计模式学习心得,设计模式
    • 我的第一篇博客,我试试怎么用,第一篇博客,试试
    • [android] 手机卫士手机定位的原理,android卫士
    • 单机搭建Android开发环境(五),单机搭建android开发
    • Android MeasuerSpce的由来及使用
    • Java虚拟机 JVM,java虚拟机jvm
    • 安卓第十七天笔记--简易版本音乐播放器,安卓第十七天

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

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