定义自定义属性 [英] Defining custom attrs

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

问题描述

我需要实现我自己的属性,比如 com.android.R.attr

I need to implement my own attributes like in com.android.R.attr

在官方文档中找不到任何内容,因此我需要有关如何定义这些属性以及如何从我的代码中使用它们的信息.

Found nothing in official documentation so I need information about how to define these attrs and how to use them from my code.

推荐答案

目前最好的文档就是源.你可以看看它这里 (attrs.xml).

Currently the best documentation is the source. You can take a look at it here (attrs.xml).

您可以在顶部 元素或 元素内部定义属性.如果我要在多个地方使用 attr,我会将它放在根元素中.请注意,所有属性共享相同的全局命名空间.这意味着即使您在 <declare-styleable> 元素内部创建了一个新属性,它也可以在元素外部使用,并且您不能创建另一个具有相同名称的不同类型的属性.

You can define attributes in the top <resources> element or inside of a <declare-styleable> element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace. That means that even if you create a new attribute inside of a <declare-styleable> element it can be used outside of it and you cannot create another attribute with the same name of a different type.

元素有两个 xml 属性 nameformat.name 允许你给它命名,这就是你最终在代码中引用它的方式,例如,R.attr.my_attribute.format 属性可以具有不同的值,具体取决于您想要的属性类型".

An <attr> element has two xml attributes name and format. name lets you call it something and this is how you end up referring to it in code, e.g., R.attr.my_attribute. The format attribute can have different values depending on the 'type' of attribute you want.

  • reference - 如果它引用了另一个资源 ID(例如,@color/my_color"、@layout/my_layout")
  • 颜色
  • 布尔值
  • 维度
  • 浮动
  • 整数
  • 字符串
  • 分数
  • enum - 通常隐式定义
  • flag - 通常是隐式定义的

您可以使用 | 将格式设置为多种类型,例如 format="reference|color".

You can set the format to multiple types by using |, e.g., format="reference|color".

enum 属性可以定义如下:

<attr name="my_enum_attr">
  <enum name="value1" value="1" />
  <enum name="value2" value="2" />
</attr>

flag 属性是相似的,除了需要定义值以便它们可以位或组合在一起:

flag attributes are similar except the values need to be defined so they can be bit ored together:

<attr name="my_flag_attr">
  <flag name="fuzzy" value="0x01" />
  <flag name="cold" value="0x02" />
</attr>

除了属性之外,还有 元素.这允许您定义自定义视图可以使用的属性.您可以通过指定 <attr> 元素来完成此操作,如果之前已定义该元素,则您无需指定 format.如果您希望重用 android 属性,例如 android:gravity,那么您可以在 name 中执行此操作,如下所示.

In addition to attributes there is the <declare-styleable> element. This allows you to define attributes a custom view can use. You do this by specifying an <attr> element, if it was previously defined you do not specify the format. If you wish to reuse an android attr, for example, android:gravity, then you can do that in the name, as follows.

自定义视图示例:

<declare-styleable name="MyCustomView">
  <attr name="my_custom_attribute" />
  <attr name="android:gravity" />
</declare-styleable>

在自定义视图上以 XML 格式定义自定义属性时,您需要做一些事情.首先,您必须声明一个命名空间来查找您的属性.您在根布局元素上执行此操作.通常只有 xmlns:android="http://schemas.android.com/apk/res/android".您现在还必须添加 xmlns:whatever="http://schemas.android.com/apk/res-auto".

When defining your custom attributes in XML on your custom view you need to do a few things. First you must declare a namespace to find your attributes. You do this on the root layout element. Normally there is only xmlns:android="http://schemas.android.com/apk/res/android". You must now also add xmlns:whatever="http://schemas.android.com/apk/res-auto".

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:whatever="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <org.example.mypackage.MyCustomView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>

最后,要访问该自定义属性,您通常会在自定义视图的构造函数中进行访问,如下所示.

Finally, to access that custom attribute you normally do so in the constructor of your custom view as follows.

public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);

  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0);

  String str = a.getString(R.styleable.MyCustomView_my_custom_attribute);

  //do something with str

  a.recycle();
}

结束.:)

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

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