极力推荐文章:欢迎收藏
Android 干货分享
本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
自定义View类实现
自定义View标签
自定义View 布局
自定义View 选择器
自定义View 素材
Activity 自定义view布局引用
Activity使用自定义View
自定义ItemToggleView 常用于Settings中,主要控制开关的开启与关闭。
自定义ItemToggleView实现效果如下:
public class ItemToggleView extends RelativeLayout {
private static final String TAG = "ItemToggleView";
private TextView tv_title;
private TextView tv_des;
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.programandroid";
private String mDesTitle;
private String mDesOff;
private String mDesOn;
private ImageView mImageView;
private boolean isOnOFF;
public ItemToggleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAttrs(attrs);
initUI(context);
}
public ItemToggleView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
initUI(context);
}
public ItemToggleView(Context context) {
super(context);
initUI(context);
}
// 多带带抽取出来的 xml--->view
private void initUI(Context context) {
View.inflate(context, R.layout.item_toggle_view, this);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_des = (TextView) findViewById(R.id.tv_des);
mImageView = (ImageView) findViewById(R.id.switch_imageview);
tv_title.setText(mDesTitle);
}
public boolean getCheck() {
return isOnOFF;
}
/**
* @param isCheck
* 传递一个选中未选中的状态变量(true 选中 false未选中)
*/
public void setCheck(boolean isCheck) {
// mSwitchControlView.setChecked(isCheck);
if (isCheck) {
tv_des.setText(mDesOn);
mImageView.setImageDrawable(getResources().getDrawable(
R.drawable.toggle_on));
} else {
tv_des.setText(mDesOff);
mImageView.setImageDrawable(getResources().getDrawable(
R.drawable.toggle_off));
}
isOnOFF = isCheck;
}
/**
* @param attrs
* 包含了属性名称和属性值的set集合
*/
private void initAttrs(AttributeSet attrs) {
// 打印属性总个数
/*
* Log.i(tag, "attrs.getAttributeCount() = "+attrs.getAttributeCount());
* for(int i=0;i
2. 自定义View标签
1.注意 :自定义 Android 命名空间
同Android 命名空间`(xmlns:android="http://schemas.android.com/apk/res/android"
)方法一样,想使用自定义view的属性,必须声明自定义view`的命名空间
(xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid")
2. 注意:自定义View 属性
自定义View 属性如下:
programandroid:desOff=" 不选中"
programandroid:desOn=" 选中"
programandroid:desTitle=" WIFI "
属性声明在res/values/attrs.xml中定义
3. 自定义View 布局
4. 自定义View 选择器
5. 自定义View 素材
6. Activity 自定义view布局引用
7. Activity使用自定义View
/**
* 自定义 ItemToggleView
*/
private void InitItemToggleView() {
// TODO Auto-generated method stub
final ItemToggleView mItemToggleView = (ItemToggleView) findViewById(R.id.custom_item_toggle_view);
mItemToggleView.setCheck(false);
mItemToggleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mItemToggleView.setCheck(!mItemToggleView.getCheck());
}
});
}
至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/75874.html
摘要:极力推荐文章欢迎收藏干货分享阅读五分钟,每日十点,和您一起终身学习,这里是程序员本篇文章主要介绍开发中的部分知识点,通过阅读本篇文章,您将收获以下内容自定义类实现自定义布局自定义素材使用自定义自定义常用于中,主要控制开关的开启与关闭。 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 极力...
摘要:变量的函数引用的自定义属性被称为变量。为此,可读性和可维护性是自定义属性最大的优势。自定义属性作用域在中,变量有作用域一说。因此,在选择器中声明的自定义属性,其作用域的范围是全局范围,也就是全局作用域。 引言 CSS语言是一种声明式语言,不像其他语言有变量、条件和逻辑等特性,因为这个原因,社区中有了各种CSS处理器语言,比如Sass、LESS和Stylus等。这些处理器语言引入了一...
摘要:若自定义元素标签名称不可用则摒弃。总之,自定义元素让开发者的代码更易理解和维护,并分割为小型,可复用及可封装的模块。被称为自定义元素接口,虽然现在仍然可用,但是已经被弃用并被认为是糟糕的实现。 原文请查阅这里,略有删减,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland。 这是 JavaScript 工作原理第十九章。 概述 在 前述文章中,我们介绍了 Shadow ...
阅读 1018·2023-04-25 17:33
阅读 3951·2021-07-29 14:49
阅读 2655·2019-08-30 15:53
阅读 3641·2019-08-29 16:27
阅读 2229·2019-08-29 16:11
阅读 1197·2019-08-29 14:17
阅读 2960·2019-08-29 13:47
阅读 2265·2019-08-29 13:28