XmlPullParser parser = getResources().getXml(R.layout.textview);
    AttributeSet attributes = Xml.asAttributeSet(parser);

int type;

while ((type = parser.next()) != XmlPullParser.START_TAG &&
            type != XmlPullParser.END_DOCUMENT) {
                    // Empty
    }

TextView tv=new TextView(this,attributes);

有时候我们需要在代码中动态创建view,并把它加入到当前的viewGroup中,动态创建view一般使用LayoutInflater或者构造函数,在这里使用构造函数,有三个构造函数可用,比如动态创建TextView,可以使用这三个构造函数:

TextView(Context context)
    TextView(Context context, AttributeSet attrs)
    TextView(Context context, AttributeSet attrs, int defStyleAttr)

其中context是当前Activity的Context,attrs是一组属性值,例如layout_width,layout_height等等,defStyleAttr和控件的style有关,本篇暂不考虑.

context很容易拿到,那么attrs是怎么拿到的呢,查看文档发现官网上有给出:

XmlPullParser parser = resources.getXml(myResouce);
    AttributeSet attributes = Xml.asAttributeSet(parser);

resources可以用context.getResources()获取,这是本地资源信息的类,myResouce是资源ID.

我们在res/layout下创建资源XML文件textview.xml:

<TextView
    xmlns:Android="http://schemas.android.com/apk/res/android"
    android:background="#ff00ff00"
    android:text="hello world!"/>

拿到这两个参数,现在就可以调用第二个构建函数创建view了吗?嗯,我也是这样想的,那么问题来了,创建view之后发现没有任何显示,这是为什么呢,试着给它写入文本,在创建后调用tv.setText("hello");这回有显示了,背景也不是我们写在XML中的值,这说明attrs没有发挥作用.推断原因可能是attrs中的值不对,调用parser.getAttributeCount()发现返回的是-1,说明XML没有被正确解析!

想起使用LayoutInflater也是使用XML文件生成了VIEW,或许可以看看它的代码:

public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }

public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
        XmlResourceParser parser = getContext().getResources().getLayout(resource);//这里和getXml效果一样
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            final AttributeSet attrs = Xml.asAttributeSet(parser); //这里也和我们之前做的一样
            Context lastContext = (Context)mConstructorArgs[0];
            mConstructorArgs[0] = mContext;
            View result = root;

try {
                // Look for the root node. !!这里是我们没有做过的!!
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }

if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }

final String name = parser.getName();
                ....更多的代码隐藏起来了

} catch (XmlPullParserException e) {
                InflateException ex = new InflateException(e.getMessage());
                ex.initCause(e);
                throw ex;
            } catch (IOException e) {
                InflateException ex = new InflateException(
                        parser.getPositionDescription()
                        + ": " + e.getMessage());
                ex.initCause(e);
                throw ex;
            } finally {
                // Don't retain static reference on context.
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;
            }

return result;
        }
    }

这里发现我们在创建attrs时,没有去查找XML的根节点:

while ((type = parser.next()) != XmlPullParser.START_TAG &&
            type != XmlPullParser.END_DOCUMENT) {
                    // Empty
    }

现在在代码中加上这段,发现XML的设置起使用了,并且parser.getAttributeCount()也返回了2,正是我们属性的个数.

总结一下流程:

XmlPullParser parser = getResources().getXml(R.layout.textview);
    AttributeSet attributes = Xml.asAttributeSet(parser);

int type;

while ((type = parser.next()) != XmlPullParser.START_TAG &&
            type != XmlPullParser.END_DOCUMENT) {
                    // Empty
    }

TextView tv=new TextView(this,attributes);

努力了这么久,只为了创建一个View,还不如用LayoutInflater呢,有人这样想吗?完全正确,我之所以这样做是因为我要获取到attributes来进行布局设置,很多例子中,生成一个view之后就直接setContentView去了,这在项目中应该很少这么干的,因为我们还有其他的view要显示,创建的view只是显示在大布局中的一块而已.我们要把这个view加入到其它的布局中去,一般调用的是addView:

void     addView(View child, int index, ViewGroup.LayoutParams params)  Adds a child view with the specified layout parameters.
    void     addView(View child, ViewGroup.LayoutParams params)  Adds a child view with the specified layout parameters.
    void     addView(View child, int index)   Adds a child view.
    void     addView(View child) Adds a child view.
    void     addView(View child, int width, int height) Adds a child view with this ViewGroup's default layout parameters and the specified width and height.

特别说明,只有继承了ViewGroup的类才有这些函数,比如RelativeLayout,LinearLayout等.child参数是我们刚刚创建的view,index是我们的view在此layout中的Z序,params是布局参数.

不同的布局类有不同的布局参数类.它们都继承自ViewGroup.LayoutParams,例如 LinearLayout.LayoutParams, RelativeLayout.LayoutParams等等,关注RelativeLayout.LayoutParams的构造函数:

RelativeLayout.LayoutParams(Context c, AttributeSet attrs)
    RelativeLayout.LayoutParams(int w, int h)  //可以指定长和宽
    RelativeLayout.LayoutParams(ViewGroup.LayoutParams source)
    RelativeLayout.LayoutParams(ViewGroup.MarginLayoutParams source)

从上面看出,我们在构造时可以设置view的长和宽,使用ViewGroup.MarginLayoutParams可以设置view的边距,其它的只能通过attrs来设置了,这就是我要创建attrs的目的.使用attrs完全可以精细化操作布局,下面来一个简单的示例,动态创建一个textView,并把它加入到主布局文件的RelativeLayout中,并局中显示:

https://blog.csdn.net/chenhuakang/article/details/53584429

安卓设置AttributeSet的更多相关文章

  1. 呈现怎样的香蕉饼路线Android系统

    无话可说,该系统的第一版,Android有的还可以,路由设置确实有闪光现象背.与其他的稳定版本发布,被能够机顶盒和路由组合.其次是SSD,还是很不错的. 硬件 watermark/2/text/aHR ...

  2. 四 APPIUM GUI讲解(Windows版)

    Windows版本的APPIUM GUI有以下图标或者按钮: ·Android Settings  - Android设置按钮,所有和安卓设置的参数都在这个里面 ·General Settings – ...

  3. appium GUI介绍

    Appium作为APP端的自动化测试工具,具有很多的有点,今天主要介绍一下它的UI界面,包含其中的一些参数等.主要说的是windows下的APPIUM GUI. 先看一眼它的界面(版本为1.4.16. ...

  4. 详解BLE连接建立过程

    同一款手机,为什么跟某些设备可以连接成功,而跟另外一些设备又连接不成功?同一个设备,为什么跟某些手机可以建立连接,而跟另外一些手机又无法建立连接?同一个手机,同一个设备,为什么他们两者有时候连起来很快 ...

  5. fiddler抓取https请求(android/ios)

    本文转载自:http://blog.csdn.net/songer_xing/article/details/53841401 备注:本人有这样的一个需求,先记录下,以后再进行整理. 在抓包过程中发现 ...

  6. Appium的inspector使用

    使用inspectot可以对元素进行定位 1.设置appium的Android Settings,点击左上角的安卓图标进入安卓设置,注意设置时不要开启appium 说明: a)Application是 ...

  7. 第一个Xamarin的 Android 应用程序!

    你好,安卓 Xamarin的工作室 Xamarin的应用程序图标和启动屏幕 脱机使用PDF格式: 介绍与Xamarin的Android开发 示例代码: 开始使用应用程序的探险家 显示说明: Visua ...

  8. 三 APPIUM GUI讲解(Windows版)

    本文本转自:http://www.cnblogs.com/sundalian/p/5629386.html APPIUM GUI讲解(Windows版)   Windows版本的APPIUM GUI有 ...

  9. 安全性测试:OWASP ZAP 2.8 使用指南(四):ZAP扫描移动应用安全

    在做移动应用(APP,小程序等)测试时,需要关注应用安全性. ZAP是可以用来进行手机移动应用渗透性测试扫描的. 正因为ZAP是采用“中间代理”的形式,截取并扫描所有客户端与服务器的交互请求,作为客户 ...

随机推荐

  1. Servlet通过JavaBean传值到JSP页面

    主要通过Attribute进行传递,主要代码如下: 赋值,并定义跳转的页面: request.setAttribute("user", user); request.getRequ ...

  2. Html5 history Api简介

    一. Html4的History API back() 后退,跟按下“后退”键是等效的. forward() 前进,跟按下“前进”键是等效的. go() 用法:history.go(x):在历史的范围 ...

  3. SfM环境的搭建windows8.1+vs2010

    SfM即Structure form Motion,这个算法的实现,作者Noah Snavely给出了一个具体的实现. 目前最新下载https://github.com/snavely/bundler ...

  4. 设计模式:浅析 抽象工厂、工厂方法、简单(静态)工厂 java实现

    ----简单工厂 (也叫静态工厂模式):一个抽象产品抽象出多个详细产品类.一个详细工厂类 代码: //抽象产品角色 public interface Car{ public void drive(); ...

  5. ORA-00600: internal error code, arguments: [kkqtSetOp.1]

    新数据库从32升级到64位的11G 11 2 0 3 有条SQL 语句运行的时候会导致内部错误. 使用PL/SQL DEVELOPER 查询该语句的运行机会 按F5键 就激发了这个ORA600 单击此 ...

  6. 两种Linux下非交互式命令的实现

    一.概述 在Linux环境,有多种实现自己主动化的脚本语言.如Shell.Python.Perl.Tcl等. Shell语言因与Shell外壳结合紧密,是最常见的实现自己主动化的脚本语言. 同一时候, ...

  7. starUML 2.5.1 for mac

    http://www.macupdate.com/app/mac/55571/staruml/download 一直在windows下使用 star UML,占用资源少,简洁易用. Mac下也能够用了 ...

  8. Ubuntu13.10添加/删除PPA

    什么是ppa ppa即Personal Package Archive(个人安装包文档),简单来说就是一些个人或者团体通过独立的网站发布的第三方的软件源.将这些软件源添加到你的电脑上后,你就可以像使用 ...

  9. Linux对外连接port数限制

    左右时,開始大量抛例如以下异常: java.net.BindException:Cannot assign requested address atsun.nio.ch.Net.connect0(Na ...

  10. 深入理解7816(2)---关于ATR【转】

    本文转载自:http://blog.sina.com.cn/s/blog_4df8400a0102vcrk.html 深入理解7816(2)---关于ATR 智能卡(此处主要指接触式CPU卡)本身始终 ...