zoukankan      html  css  js  c++  java
  • Android 自定义view中的属性,命名空间,以及tools标签

    昨日看到有人在知乎上问这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 }

    最后看下我们的输出。

  • 相关阅读:
    NConsoler 介绍
    HOWTO:批量删除存储过程和表
    HOWTO:使ASP.NET网站Forms验证可以指定多个登录页面
    [架构模式实践]如何不让第三方服务/组件的故障阻碍开发和测试进度
    GDI+学习笔记
    脚印: SD2C 2009 参会小记(非技术篇)
    Expression Web使用问题,相关资源及今日阅读
    HOWTO:FirePHP乱码问题解决
    [ECSHOP挖寶]用戶注銷過程
    励志好文
  • 原文地址:https://www.cnblogs.com/punkisnotdead/p/4739668.html
Copyright © 2011-2022 走看看