问题描述
在 android 中创建自定义组件时,经常会被问到如何创建 attrs 属性并将其传递给构造函数.
When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor.
通常建议在java中创建组件时,只需使用默认构造函数,即
It is often suggested that when creating a component in java that you simply use the default constructor, i.e.
new MyComponent(context);
而不是尝试创建一个 attrs 对象以传递给基于 xml 的自定义组件中常见的重载构造函数.我试图创建一个 attrs 对象,但它似乎既不容易或根本不可能(没有非常复杂的过程),而且从所有角度来看,这并不是真正需要的.
rather than attempting to create an attrs object to pass through to the overloaded constructor often seen in xml based custom components. I've tried to create an attrs object and it doesn't seem either easy or at all possible (without an exceedingly complicated process), and by all accounts isn't really required.
然后我的问题是:在 java 中构造一个自定义组件的最有效方法是什么,该组件传递或设置在使用 xml 对组件进行膨胀时本来由 attrs 对象设置的属性?
My question is then: What is the most efficient way of construction a custom component in java that passes or sets properties that would have otherwise been set by the attrs object when inflating a component using xml?
推荐答案
(完全披露:这个问题是创建自定义视图)
(Full disclosure: This question is an offshoot of Creating custom view)
除了从 View
继承的三个标准构造函数之外,您还可以创建添加所需属性的构造函数...
You can create constructors beyond the three standard ones inherited from View
that add the attributes you want...
MyComponent(Context context, String foo)
{
super(context);
// Do something with foo
}
...但我不推荐它.最好遵循与其他组件相同的约定.这将使您的组件尽可能灵活,并防止使用您的组件的开发人员因为您的组件与其他所有内容不一致而撕毁他们的头发:
...but I don't recommend it. It's better to follow the same convention as other components. This will make your component as flexible as possible and will prevent developers using your component from tearing their hair out because yours is inconsistent with everything else:
1.为每个属性提供 getter 和 setter:
public void setFoo(String new_foo) { ... }
public String getFoo() { ... }
<强>2.在 res/values/attrs.xml
中定义属性,以便它们可以在 XML 中使用.
2. Define the attributes in res/values/attrs.xml
so they can be used in XML.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyComponent">
<attr name="foo" format="string" />
</declare-styleable>
</resources>
3.提供 View
中的三个标准构造函数.
3. Provide the three standard constructors from View
.
如果您需要从一个采用 AttributeSet
的构造函数中的属性中挑选任何内容,您可以这样做...
If you need to pick anything out of the attributes in one of the constructors that takes an AttributeSet
, you can do...
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MyComponent);
CharSequence foo_cs = arr.getString(R.styleable.MyComponent_foo);
if (foo_cs != null) {
// Do something with foo_cs.toString()
}
arr.recycle(); // Do this when done.
完成所有这些后,您就可以通过编程方式实例化 MyCompnent
...
With all that done, you can instantiate MyCompnent
programmatically...
MyComponent c = new MyComponent(context);
c.setFoo("Bar");
...或通过 XML:
...or via XML:
<!-- res/layout/MyActivity.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:blrfl="http://schemas.android.com/apk/res-auto"
...etc...
>
<com.blrfl.MyComponent
android:id="@+id/customid"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
blrfl:foo="bar"
blrfl:quux="bletch"
/>
</LinearLayout>
其他资源 - https://developer.android.com/training/custom-views/create-view
这篇关于如何在 java 和 xml 中传递自定义组件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!