【发布时间】:2012-04-19 10:26:28
【问题描述】:
我添加了一个简单的复合控件,它从 XML 布局加载其布局并实现自己的 XML 属性。
但这仅在此控件位于主包 (com.myapp) 中时才有效。当我尝试将它移动到像 com.myapp.controls 这样的子包中时,事情开始失败。
这是定义控件自定义属性的attrs.xml:
<resources>
<declare-styleable name="CH2">
<attr name="textText" format="string" />
</declare-styleable>
<declare-styleable name="CH3">
<attr name="textFex" format="string" />
</declare-styleable>
</resources>
这是子包中的控件 CH2.java,它不工作:
package com.myapp.controls;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CH2 extends LinearLayout {
private TextView textView;
public CH2(Context context) throws Exception {
super(context);
init(context, null);
}
public CH2(Context context, AttributeSet attrs) throws Exception {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) throws Exception {
this.setOrientation(LinearLayout.VERTICAL);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(com.myapp.R.layout.category_header, this, true);
View v = findViewById(com.myapp.R.id.chText);
this.textView = (TextView)v;
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, com.myapp.R.styleable.CH2);
String text = a.getString(com.myapp.R.styleable.CH2_textText);
this.textView.setText(text != null ? text : "<NULL!>");
a.recycle();
}
}
}
这是工作的 CH3.java:
package com.myapp;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CH3 extends LinearLayout {
private TextView textView;
public CH3(Context context) throws Exception {
super(context);
init(context, null);
}
public CH3(Context context, AttributeSet attrs) throws Exception {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) throws Exception {
this.setOrientation(LinearLayout.VERTICAL);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(com.myapp.R.layout.category_header, this, true);
View v = findViewById(com.myapp.R.id.chText);
this.textView = (TextView)v;
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, com.myapp.R.styleable.CH3);
String text = a.getString(com.myapp.R.styleable.CH3_textFex);
this.textView.setText(text != null ? text : "<NULL!>");
a.recycle();
}
}
}
两个来源使用相同的 category_header.xml。布局加载没有问题。我可以像这样使用两者:
<com.myapp.controls.CH2
android:id="@+id/cH1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
myns:textText="Test 1" >
</com.myapp.controls.CH2>
<com.myapp.CH3
android:id="@+id/cH2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
myns:textFex="Test 2"
>
</com.myapp.CH3>
如何使 CH2 工作?还是无法使用其他包?
Sidequest:我可以让两个控件使用相同的 XML 属性 textText 还是它们总是必须使用不同的属性?或者可声明样式的名称可以与组件名称不同吗?
另一种使用新的 declare-styleable 的方法:
<declare-styleable name="controls.CH2">
<attr name="textText" format="string" />
</declare-styleable>
但是好像不能用。无论我如何使用 CH2,XML 视图总是显示 Unexpected text found in layout file::
xmlns:myns="http://schemas.android.com/apk/res/com.myapp"
xmlns:mynsc="http://schemas.android.com/apk/res/com.myapp.controls"
...
<com.myapp.controls.CH2
android:id="@+id/cH1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
mynsc:textText="TestTestTest"
>
</com.myapp.controls.CH2>
或
xmlns:myns="http://schemas.android.com/apk/res/com.myapp"
...
<com.myapp.controls.CH2
android:id="@+id/cH1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
myns.controls:textText="TestTestTest"
>
</com.myapp.controls.CH2>
【问题讨论】:
-
第一种方法的错误是什么?只是参数没有通过?
-
你说得对,这行得通。一开始它只是没有,因为有一个 XML 错误(eclipse 无法识别)。所以这很完美!我只需要清理项目并重新启动 eclipse 即可使所有命名空间更改生效。
标签: android namespaces custom-controls packages