声明使用XML定制的Andr​​oid UI元素元素、声明、XML、Andr

2023-09-11 10:36:18 作者:ゝ*‵敗類氾濫

如何,我宣布一个Android UI元素使用XML?

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>

注意在使用不合格的名称的申报,设置样式标记。如 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 包含 android_text 属性 MyCustomView 。属性可以被检索到的 TypedArray 使用各种 GET 功能。如果属性没有在XML定义的定义,那么返回。除,当然,如果返回类型是基本,在这种情况下,第二个参数被返回。

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.

如果您不希望检索所有的属性,就可以创建这个数组manually.The ID为标准Android属性包含在 android.R.attr ,而对于这个项目的属性在 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 使用任何东西,按this螺纹的,可能在未来改变。它仍然是在文档中作为可查看所有这些常量的一个地方是很有用的。

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.

包括命名空间声明的xmlns:在顶级XML元素的应用程序=htt​​p://schemas.android.com/apk/res-auto。命名空间提供了一种方法,以避免发生冲突时,不同的模式使用相同的元素名称,有时会发生(见this文章更多信息)。该URL仅仅是唯一标识架构的方式 - 没有什么实际需要在该URL被托管。如果这似乎没有做任何事情,那是因为你实际上并不需要添加命名空间preFIX,除非你需要解决冲突。

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:程序=htt​​p://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"

StackOverflow主题:检索定制控制 XML属性 How我用obtainStyledAttributes与Android 的内部主题 定义自定义属性支持的属性格式 的+列表 StackOverflow Thread: Retrieving an XML attribute for custom control How do I use obtainStyledAttributes with internal themes of Android Defining custom attributes + list of supported attribute formats