浅析Android手机卫士自定义控件的属性

 更新时间:2016年04月08日 14:57:11   作者:陶士涵  
这篇文章主要介绍了浅析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>

以上是针对Android手机卫士自定义控件的属性相关介绍,希望对大家有所帮助!

相关文章

  • Android悬浮窗的实现步骤

    Android悬浮窗的实现步骤

    最近想做一个悬浮窗秒表的功能,所以看下悬浮窗具体的实现步骤,接下来通过本文给大家介绍Android悬浮窗的实现,需要的朋友可以参考下
    2024-01-01
  • Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

    Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

    这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
    2018-05-05
  • Android 监听手机GPS打开状态实现代码

    Android 监听手机GPS打开状态实现代码

    这篇文章主要介绍了Android 监听手机GPS打开状态实现代码的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android使用第三方服务器Bmob实现发送短信验证码

    Android使用第三方服务器Bmob实现发送短信验证码

    这篇文章主要介绍了Android使用第三方服务器Bmob实现发送短信验证码的思路详解,需要的朋友可以参考下
    2016-09-09
  • Android实现Android APP自动更新功能

    Android实现Android APP自动更新功能

    在移动应用的全生命周期中,版本迭代和用户更新体验至关重要,传统的做法是依赖 Google Play 商店强制推送更新,但在某些场景下,我们需要更即时地控制更新流程,所以本文给大家介绍了Android实现Android APP自动更新功能,需要的朋友可以参考下
    2025-04-04
  • Gradle的缓存路径修改的四种方法(小结)

    Gradle的缓存路径修改的四种方法(小结)

    这篇文章主要介绍了Gradle的缓存路径修改的四种方法(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • ijkplayer打包支持https的so使用详解

    ijkplayer打包支持https的so使用详解

    这篇文章主要为大家介绍了ijkplayer打包支持https的so使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android App页面滑动标题栏颜色渐变详解

    Android App页面滑动标题栏颜色渐变详解

    这篇文章主要为大家详细介绍了Android App页面滑动标题栏颜色渐变,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Android 百分比布局详解及实例代码

    Android 百分比布局详解及实例代码

    这篇文章主要介绍了Android 百分比布局详解及实例代码的相关资料,这里附有代码实例帮助大家学习参考,如何实现百分比布局,需要的朋友可以参考下
    2016-11-11
  • Android ImageView 固定宽高比例的实现方法

    Android ImageView 固定宽高比例的实现方法

    这篇文章主要介绍了Android ImageView 固定宽高比例的实现方法的相关资料,,方法一:设置 adjustViewBounds="true",方法二:使用 Universal-Image-Loader 图片缓存类,需要注意的是方法二和方法一同时使用导致设置无效,需要的朋友可以参考下
    2017-07-07

最新评论