我一直想一个TextView和一个EditText组合成一个复合控件,它使用自定义XML元素来传递默认值为每个单独的元素。我一直在寻找教程/文档浏览: Building复合控件 Passing自定义属性
我有这么远。
Attrs.xml:
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<申报,设置样式名称=FreeText的>
< attr指示NAME =标签格式=字符串/>
< attr指示NAME =默认格式=字符串/>
< /申报,设置样式>
< /资源>
我的主要布局:
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:的myapp =http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
< com.example.misc.FreeText
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
的myapp:标签=标签
的myapp:默认=默认
/>
< / LinearLayout中>
我的复合控制,FreeText的:
公共类FreeText的扩展的LinearLayout {
TextView的标签;
的EditText值;
公共FreeText的(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
this.setOrientation(水平);
的LayoutParams LP =新的LayoutParams(0,LayoutParams.WRAP_CONTENT);
lp.weight = 1;
标签=新的TextView(上下文);
addView(标签,LP);
值=新的EditText(上下文);
addView(值,LP);
TypedArray A = context.obtainStyledAttributes(ATTRS,R.styleable.FreeText);
CharSequence中= a.getString(R.styleable.FreeText_label);
如果(S!= NULL){
label.setText(多个);
}
a.recycle();
}
}
当我运行程序我看到了意见确定,但我的CharSequence,s的值,始终为空。谁能告诉我,我要去哪里错了?
解决方案我恨它,你寻求帮助之后,当你注意到这个问题。
但问题是,我的空间我的自定义XML元素本来应该像这样:
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:的myapp =http://schemas.android.com/apk/res-auto
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
< com.example.misc.FreeText
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
的myapp:标签=标签
的myapp:默认=默认
/>
< / LinearLayout中>
I'm been trying to combine a TextView and an EditText into one compound control which uses custom xml elements to pass in default values for each individual element. I've been looking at the tutorials/docs here: Building Compound Controls Passing Custom Attributes
What I have so far.
Attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FreeText">
<attr name="label" format="string" />
<attr name="default" format="string" />
</declare-styleable>
</resources>
My Main Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.misc.FreeText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:label="label"
myapp:default="default"
/>
</LinearLayout>
My Compound Control, FreeText:
public class FreeText extends LinearLayout {
TextView label;
EditText value;
public FreeText(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOrientation(HORIZONTAL);
LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
lp.weight = 1;
label = new TextView(context);
addView(label, lp);
value = new EditText(context);
addView(value, lp);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FreeText);
CharSequence s = a.getString(R.styleable.FreeText_label);
if (s != null) {
label.setText(s);
}
a.recycle();
}
}
When I run the program I see the views OK but the value of my CharSequence, s, is always null. Can someone tell me where I'm going wrong?
解决方案I hate it when you notice the problem right after you ask for help.
The problem was that my namespace for my custom XML elements should have been like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.misc.FreeText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:label="label"
myapp:default="default"
/>
</LinearLayout>