使用 XML 声明自定义 android UI 元素 [英] Declaring a custom android UI element using XML

查看:43
本文介绍了使用 XML 声明自定义 android UI 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 XML 声明 Android UI 元素?

How do I declare an Android UI element using XML?

推荐答案

Android 开发人员指南有一个名为 构建自定义组件.不幸的是,有关 XML 属性的讨论仅包括声明布局文件中的控件,而不是实际处理类初始化中的值.步骤如下:

The Android Developer Guide has a section called Building Custom Components. Unfortunately, the discussion of XML attributes only covers declaring the control inside the layout file and not actually handling the values inside the class initialisation. The steps are as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="android:text"/>
        <attr name="android:textColor"/>            
        <attr name="extraInformation" format="string" />
    </declare-styleable>
</resources>

请注意在 declare-styleable 标记中使用了非限定名称.像 extraInformation 这样的非标准 android 属性需要声明它们的类型.在超类中声明的标签将在子类中可用,而无需重新声明.

Notice the use of an unqualified name in the declare-styleable tag. Non-standard android attributes like extraInformation need to have their type declared. Tags declared in the superclass will be available in subclasses without having to be redeclared.

由于有两个构造函数使用 AttributeSet 进行初始化,因此可以方便地创建单独的初始化方法供构造函数调用.

Since there are two constructors that use an AttributeSet for initialisation, it is convenient to create a separate initialisation method for the constructors to call.

private void init(AttributeSet attrs) { 
    TypedArray a=getContext().obtainStyledAttributes(
         attrs,
         R.styleable.MyCustomView);

    //Use a
    Log.i("test",a.getString(
         R.styleable.MyCustomView_android_text));
    Log.i("test",""+a.getColor(
         R.styleable.MyCustomView_android_textColor, Color.BLACK));
    Log.i("test",a.getString(
         R.styleable.MyCustomView_extraInformation));

    //Don't forget this
    a.recycle();
}

R.styleable.MyCustomView 是一个自动生成的 int[] 资源,其中每个元素都是一个属性的 ID.通过将属性名称附加到元素名称,为 XML 中的每个属性生成属性.例如,R.styleable.MyCustomView_android_text 包含 MyCustomViewandroid_text 属性.然后可以使用各种 get 函数从 TypedArray 中检索属性.如果该属性未在 XML 中定义,则返回 null.当然,除非返回类型是原始类型,否则将返回第二个参数.

R.styleable.MyCustomView is an autogenerated int[] resource where each element is the ID of an attribute. Attributes are generated for each property in the XML by appending the attribute name to the element name. For example, R.styleable.MyCustomView_android_text contains the android_text attribute for MyCustomView. Attributes can then be retrieved from the TypedArray using various get functions. If the attribute is not defined in the defined in the XML, then null is returned. Except, of course, if the return type is a primitive, in which case the second argument is returned.

如果你不想检索所有的属性,可以手动创建这个数组.标准android属性的ID包含在android.R.attr中,而attributes for这个项目在 R.attr.

If you don't want to retrieve all of the attributes, it is possible to create this array manually.The ID for standard android attributes are included in android.R.attr, while attributes for this project are in R.attr.

int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor};

请注意,您应该不要android.R.styleable 中使用任何内容,根据 该线程 将来可能会改变.它仍在文档中,因为在一个地方查看所有这些常量很有用.

Please note that you should not use anything in android.R.styleable, as per this thread it may change in the future. It is still in the documentation as being to view all these constants in the one place is useful.

在顶级 xml 元素中包含命名空间声明 xmlns:app="http://schemas.android.com/apk/res-auto".命名空间提供了一种方法来避免不同模式使用相同元素名称时有时会发生的冲突(参见 这篇文章 了解更多信息).URL 只是一种唯一标识模式的方式 - 实际上不需要在该 URL 上托管任何内容.如果这看起来没有任何作用,那是因为您实际上并不需要添加命名空间前缀,除非您需要解决冲突.

Include the namespace declaration xmlns:app="http://schemas.android.com/apk/res-auto" in the top level xml element. Namespaces provide a method to avoid the conflicts that sometimes occur when different schemas use the same element names (see this article for more info). The URL is simply a manner of uniquely identifying schemas - nothing actually needs to be hosted at that URL. If this doesn't appear to be doing anything, it is because you don't actually need to add the namespace prefix unless you need to resolve a conflict.

<com.mycompany.projectname.MyCustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:text="Test text"
    android:textColor="#FFFFFF"
    app:extraInformation="My extra information"
/> 

使用完全限定名称引用自定义视图.

Reference the custom view using the fully qualified name.

如果你想要一个完整的例子,请查看android标签视图示例.

If you want a complete example, look at the android label view sample.

LabelView.java

 TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView);
 CharSequences=a.getString(R.styleable.LabelView_text);

attrs.xml

<declare-styleable name="LabelView">
    <attr name="text"format="string"/>
    <attr name="textColor"format="color"/>
    <attr name="textSize"format="dimension"/>
</declare-styleable>

custom_view_1.xml

<com.example.android.apis.view.LabelView
    android:background="@drawable/blue"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:text="Blue" app:textSize="20dp"/>

这包含在具有命名空间属性的 LinearLayout 中:xmlns:app="http://schemas.android.com/apk/res-auto"

This is contained in a LinearLayout with a namespace attribute: xmlns:app="http://schemas.android.com/apk/res-auto"

这篇关于使用 XML 声明自定义 android UI 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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