原文:http://www.w2bc.com/Article/59242
昨日看到有人在知乎上问这3个琐碎的小知识点,今天索性就整理了一下,其实这些知识点并不难,但是很多开发者平时很少注意到这些,
导致的后果就是开发的时候 经常会被ide报错,开发效率很低,或者看开源代码的时候很多地方看不懂。
考虑到现在越来越多的人开发环境迁移到android studio,所以一切以android studio环境为准。和eclipse开发环境相比其实两者是差不多的,
偶有区别 主要也是android studio引入的gradle脚本造成差异。
首先来看看tools标签。
这个地方很多人不明白xmlns:tools 这行代码是干嘛的,好像删除了以后对程序也没么影响,实际上这个tools标签主要是为adt插件使用的。
他里面的很多属性能在很大程度上方便我们的开发,但是并不会影响我们最终生成的apk包。比如大家在写一个界面的时候一般都会给
textview写上text的值,然后在开发完毕的时候再删除他,这个操作就很麻烦,但是现在你就可以。
如果加上tools:text 你就可以在界面预览中看到效果,但是实际运行时是不会有效果的。很方便的,同样的以往我们在开发listview的时候之所以累就是无法预览listview的item效果,
每次都得运行以后才能看到。但是现在你只需要利用tools标签。
然后你无需run你的程序 直接在界面预览就能看到item的效果
官方给出的文档在这里 http://tools.android.com/tech-docs/tools-attributes
有兴趣的同学可以上去自己看看,试试看这些标签,对开发速度会有显著的提升的~~
另外再说下 res和res-auto的区别。
1 xmlns:android="http://schemas.android.com/apk/res/android" 2 3 xmlns:customview="http://schemas.android.com/apk/res-auto"
这2个实际上前者是就是让你引用系统自带属性的,后者是让你使用lib库里自定义属性的。
但是这个地方要注意,在eclipse中如果要使用你自定义的属性 是不能用res-auto的
必须得替换成你自定义view所属的包名,如果你在恰好使用的自定义属性被做成了lib
那就只能使用res-auto了,而在android-studio里,无论你是自己写自定义view
还是引用的lib里的自定义的view 都只能使用res-auto这个写法。以前那个包名的写法
在android-studio里是被废弃无法使用的。
最后我们来看看TypedArray和attrs之间的区别异同以及在自定义view里的应用。
首先我们自定义几个属性
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="attrName"> 4 <attr name="name" format="string"></attr> 5 <attr name="number" format="integer"></attr> 6 </declare-styleable> 7 8 9 </resources>
然后布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:customview="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.administrator.popupmenu.CustomView android:layout_width="100dp" android:layout_height="100dp" android:padding="@dimen/padding" customview:name="@string/hello_world" customview:number="123" /> </LinearLayout>
然后看下自定义view的源码
1 package com.example.administrator.popupmenu; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.util.AttributeSet; 6 import android.util.Log; 7 import android.view.View; 8 9 /** 10 * Created by Administrator on 2015/8/18. 11 */ 12 public class CustomView extends View { 13 14 private static final String TAG = CustomView.class.getSimpleName(); 15 16 public CustomView(Context context, AttributeSet attrs) { 17 super(context, attrs); 18 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.attrName); 19 String name = ta.getString(R.styleable.attrName_name); 20 int number = ta.getInteger(R.styleable.attrName_number, -1); 21 Log.e(TAG, "name=" + name + " number=" + number); 22 23 /** 24 * attrs在取值的时候 缺陷就是如果值里面还有类似的引用 则取不到正确的值 25 * 需要额外 26 * 27 */ 28 for (int i = 0; i < attrs.getAttributeCount(); i++) { 29 Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + attrs.getAttributeValue(i)); 30 //取出來實際的像素的值 31 if (attrs.getAttributeName(i).equals("padding")) { 32 ; 33 Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + getResources().getDimension(attrs.getAttributeResourceValue(i, -1))); 34 35 } 36 //这个地方就能看出来TypedArray比attrs要好用的多~同时也可以理解两者区别了 37 if (attrs.getAttributeName(i).equals("name")) { 38 ; 39 Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + getResources().getString(attrs.getAttributeResourceValue(i, -1))); 40 41 } 42 } 43 ta.recycle(); 44 45 46 } 47 }
最后看下我们的输出。
相关推荐
android 自定义view及自定义属性
android 自定义view比较综合的例子,涉及到一些复杂的实现效果,有一些基础的人可以参考下。
android自定义View实现SwitchView
android 自定义View并添加属性
Android中自定义View操作Android中自定义View操作Android中自定义View操作
Android自定义View实现转盘旋转的效果
android 自定义View界面大合集
仿真书籍翻页效果BookPageView简介:实现了仿真翻页效果,教程完整地描述了翻页原理...实现了覆盖翻页效果,同时教程中也讲述了页面内容的封装过程教程博客:Android自定义View——从零开始实现覆盖翻页效果效果展示:
Android自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View...
创建全新的视图将满足我们独特的UI需求。 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图。
Android 自定义view,自定义属性demo
本文利用SrcIn实现一个人脸扫描的效果,步骤如下: 1、准备两个人脸照片(一明一暗),暗照片作为背景,亮照片作为src前景显示 2、然后通过SrcIn用一个透明矩形框(Dst)与亮照片(src)取交集即可得出一个亮照片的...
根据鸿洋大神博客所写。自己的学习笔记。自定义View实现验证码。
在我们从Android小学生 进阶到 Android中学生的路上,肯定需要经历 自定义View。 大神已经给出了,最精简的Demo,我这里 顺水推舟,把一些基础知识 标记在代码之中,各位可以一边看源码 一边学基础。 觉得文章有用,...
android自定义View之NotePad出鞘记,自定义NotePad,android自定义笔记本
NULL 博文链接:https://a-bin.iteye.com/blog/1183227
用自定义View的方式来实现圆形的遥控器菜单。更多关于android自定义的知识可访问:http://blog.sina.com.cn/s/blog_9c4f0e790102vyt3.html
android使用自定义view和自定义button实现的小demo,可以实现button的点击变换背景等功能
Android自定义View以及layout属性全攻略.txt
android项目下对图片高斯模糊毛玻璃,自定义view,模糊程度可控制,高斯模糊毛玻璃,自定义view,模糊程度可控制