如何通过在Java和XML的自定义组件参数自定义、组件、参数、Java

2023-09-12 01:42:36 作者:请输入

在创建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的组件,您只需使用默认的构造函数,当IE

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.

我的问题是,那么:什么是建筑传递或设置将否则被设置在ATTRS特性在Java中的自定义组件的最有效的方式使用XML充气组件时反对

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)

您可以创建超出了三个标准的人从查看是添加你想要的属性...

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
}

...但我不建议这样做。这是更好地遵循其它部件相同的约定。这将使你的组件尽可能灵活,并会使用你的组件prevent开发商撕裂自己的头发了,因为你是一切不一致的:

...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 /价值/ 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。从查看提供了三个标准的构造函数。

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>