没有 attrs.xml 文件的自定义 xml 属性

Posted

技术标签:

【中文标题】没有 attrs.xml 文件的自定义 xml 属性【英文标题】:Custom xml attributes without attrs.xml file 【发布时间】:2012-07-18 16:04:19 【问题描述】:

最近我遇到了在 xml 布局中将自定义 xml 参数添加到我的视图中的问题。我知道我应该为此目的使用 attrs.xml 文件,但是......我发现,我可以使用自定义参数而根本没有任何 attrs.xml 文件。有人可以解释一下吗?这是一个错误还是一个有效的案例?

这是我的自定义视图:

public class TestView extends View 

public TestView(final Context context, final AttributeSet attrs) 
    this(context, attrs, 0);


public TestView(final Context context, final AttributeSet attrs, final int defStyle) 
    super(context, attrs, defStyle);
    final String scheme = "http://red.com/ui/scheme";
    if (attrs != null) 
        Log.d("TestView", "custom param value: " + attrs.getAttributeValue(scheme, "cutom"));
    

这里是主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:red="http://red.com/ui/scheme"
android:layout_
android:layout_
android:orientation="vertical" >

<TextView
    android:layout_
    android:layout_
    android:text="@string/hello" />

<com.red.ui.TestView 
    android:layout_
    android:layout_
    android:background="#ffAABBCC"
    red:cutom="customvalue"
    />

 </LinearLayout>

这是一个简单的临时项目,由 Android 向导创建。

【问题讨论】:

我一直在寻找类似的东西,因为我发现需要“attrs.xml”有点烦人。在项目之间共享自定义视图似乎变得更加复杂。但是,您的上述解决方案对我不起作用:当我从 attrs.xml 中删除我的自定义属性时,布局 xml 将无法编译......它显示“包 'y' 中属性 'x' 没有资源标识符”,表示该属性必须在 attrs.xml 中声明,否则解析器将无法识别它。 【参考方案1】:

您添加的自定义属性在 R.java 中不可用 我认为制作自定义属性的主要座右铭是在多个地方使用它。 但是通过这段代码我们无法完成同样的场景。

这里是示例代码 - attrs.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="MyLayout">
    <attr name="text" format="string" />
 </declare-styleable>
</resources>

我正在更改 main.xml 以添加 text 属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:red="http://red.com/ui/scheme"
  xmlns:myapp="http://schemas.android.com/apk/res/com.psl"
  android:layout_
  android:layout_
  android:orientation="vertical" >

<TextView
    android:layout_
    android:layout_
    android:text="@string/hello"
    myapp:text="Text String" />

<com.psl.TestView
    android:layout_
    android:layout_
    android:background="#ffAABBCC"
    myapp:text="My Special Text String"
    red:cutom="customvalue" />

</LinearLayout>

TestView.java -

public class TestView extends View 

public TestView(final Context context, final AttributeSet attrs) 
this(context, attrs, 0);


public TestView(final Context context, final AttributeSet attrs, final int defStyle) 
super(context, attrs, defStyle);
final String scheme = "http://red.com/ui/scheme";
if (attrs != null) 
    Log.d("TestView", "custom param value: " + attrs.getAttributeValue(scheme, "cutom"));


TypedArray a = context.obtainStyledAttributes(attrs,
        R.styleable.MyLayout);
CharSequence s = a.getString(R.styleable.MyLayout_text);
Log.d("MyTestView", "attrs param value: " + s.toString());


如果您在 attrs.xml 中创建 attr 后注意到。它随处可见。 但是通过自定义命名空间在 xml 本身中定义的 attr 只能通过您必须在任何地方定义的命名空间使用。 可能是它的一个错误,因为该属性被添加到一些自定义命名空间而不是应用程序本身。

【讨论】:

嗨,尽量不要将问题发布为答案。 目标:从我的自定义视图中读取我的自定义 xml 属性(例如,使其只能通过 xml 布局进行配置)。但根据我读过的任何教程:staticallytyped.wordpress.com/2011/04/16/…、kevindion.com/2011/01/custom-xml-attributes-for-android-widgets 等等,为了实现这个目标,我必须在 attrs.xml 文件中声明我的自定义属性。但是,我发现这不是必需的。我在前面展示了这种可能性。所以问题是这种用法(没有 atts 文件)是否被允许?或者它是某种黑客行为? 这个答案有用吗?或者你还有什么问题 是的,我认为这很有帮助......但是,事实证明,唯一的区别是使用 R 类访问属性时的一种“方便”?每次我想在视图中使用自定义参数时,我都没有看到任何声明命名空间的问题。或者,它只是关于指定一组支持的属性及其类型,以便任何人都知道他应该使用样式访问在属性中寻找什么类型?使用样式访问的显着差异(以及为什么它更容易和方便的关键点)对我来说现在非常模糊。 调用一次比较方便。但是,如果您必须在代码中指定命名空间 100 次。那么它会变得笨拙。您应该练习将其添加到 attrs.xml 中。然后将 attr 添加到您的应用程序本身。【参考方案2】:

这当然不是“错误”。这就是您在 xml 中使用自定义视图的方式。 参考这个:http://developer.android.com/guide/topics/ui/custom-components.html

【讨论】:

我发现相关的一件事是:“定义中的其他属性和参数是传递给自定义组件构造函数的,然后传递给EditText构造函数的,所以它们是相同的参数您将用于 EditText 视图。请注意,也可以添加您自己的参数,我们将在下面再次谈到这一点。”。但这几乎是最后一句话......它没有太大帮助。

以上是关于没有 attrs.xml 文件的自定义 xml 属性的主要内容,如果未能解决你的问题,请参考以下文章

继承ViewGroup自定义View:步骤attrs.xmlTypedArray

Android中的自定义属性attrs

Android——自定义View

android studio attrs.xml在哪儿

从资源中设置 xml 中枚举的值

attrs.xml自定义属性学习笔记(自定义View2)