Android的 - 自定义属性自定义UI [英] Android - custom UI with custom attributes

查看:205
本文介绍了Android的 - 自定义属性自定义UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它是可以创建自定义的UI元素(通过查看的方式或特定的用户界面元素扩展)。但是,它可以定义新的属性或属性新创建的用户界面元素(我的意思是不能继承的,而是全新的定义,我不能够处理好与默认propertis或属性的一些特定的行为)

I know it is possible to create custom UI element (by way of View or specific UI element extension). But is it possible to define new properties or attributes to newly created UI elements (I mean not inherited, but brand new to define some specific behavior I am not able to handle with default propertis or attributes)

例如。元素我的自定义元素:

e.g. element my custom element:

<com.tryout.myCustomElement
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Element..."
   android:myCustomValue=<someValue>
/>

那么,是否可以定义的 MyCustomValue

THX

推荐答案

是的。短指南:

创建内部 /res/values​​/attrs.xml 一个新的XML文件,与属性,它的类型

Create a new XML file inside /res/values/attrs.xml, with the attribute and it's type

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="MyCustomElement">
        <attr name="distanceExample" format="dimension"/>
    </declare-styleable>
</resources>

基本上,你必须建立一个&LT;申报-设置样式/&GT; 您的观点,即包含了所有的自定义属性(这里只有一个)。我从来没有发现可能的类型的完整列表,所以你需要看一下源一个我猜。我知道类型的引用(到另一个资源),颜色,布尔型,尺寸,浮点,整数和字符串的。他们是pretty的不言自明的。

Basically you have to set up one <declare-styleable /> for your view that contains all your custom attributes (here just one). I never found a full list of possible types, so you need to look at the source for one I guess. Types that I know are reference (to another resource), color, boolean, dimension, float, integer and string. They are pretty self-explanatory

这工作你在上面做了同样的方式,但有一个例外。您的自定义属性需要它自己的XML命名空间。

That works the same way you did above, with one exception. Your custom attribute needs it's own XML namespace.

<com.example.yourpackage.MyCustomElement
   xmlns:customNS="http://schemas.android.com/apk/res/com.example.yourpackage"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Element..."
   customNS:distanceExample="12dp"
   />

pretty的直线前进。

Pretty straight forward.

修改自定义视图来分析值的构造函数。

Modify the constructor of your custom view to parse the values.

public MyCustomElement(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomElement, 0, 0);
    try {
        distanceExample = ta.getDimension(R.styleable.MyCustomElement_distanceExample, 100.0f);
    } finally {
        ta.recycle();
    }
    // ...
}

distanceExample 在这个例子中的私有成员变量。 TypedArray 得到了很多的其他的事情来分析其他类型的值。

distanceExample is a private member variable in this example. TypedArray got lot's of other things to parse other types of values.

就是这样。使用分析得到的值在查看来修改它,例如:使用它在的OnDraw()相应地改变它的外表。

And that's it. Use the parsed value in your View to modify it, e.g. use it in onDraw() to change the look accordingly.

这篇关于Android的 - 自定义属性自定义UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆