在Android中映射XML属性和代码方法
我正在尝试创建自定义 Android 复合视图,以下是代码:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/linearLayout1">
<ImageView android:src="@drawable/icon"
android:id="@+id/action_imageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/action_text" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:text="TextView">
</TextView>
</LinearLayout>
</merge>
ActionWidget.java (我正在处理的组件):
public class ActionWidget extends LinearLayout
{
...
public ActionWidget(Context context, AttributeSet attributeSet,
int defStyle)
{
super(context, attributeSet);
LayoutInflater inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.action, this);
setClickable(true);
}
public void setLabel(String label)
{
TextView text = (TextView)findViewById(R.id.action_text);
text.setText(label);
}
public String getLabel()
{
return this.label;
}
...
}
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ActionWidget">
<attr name="label" format="string" />
<attr name="image" format="integer" />
</declare-styleable>
</resources>
用法:
<com.someapp.form.ActionWidget
android:id="@+id/actionWidget1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:label="Text changed">
</com.someapp.form.ActionWidget>
当我启动应用程序时,文本仍然是“TextView”。 我应该怎么做才能让它从 xml 加载?
I'm trying to create a custom Android compound view, here is the code:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/linearLayout1">
<ImageView android:src="@drawable/icon"
android:id="@+id/action_imageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/action_text" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:text="TextView">
</TextView>
</LinearLayout>
</merge>
ActionWidget.java (the component I'm working on):
public class ActionWidget extends LinearLayout
{
...
public ActionWidget(Context context, AttributeSet attributeSet,
int defStyle)
{
super(context, attributeSet);
LayoutInflater inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.action, this);
setClickable(true);
}
public void setLabel(String label)
{
TextView text = (TextView)findViewById(R.id.action_text);
text.setText(label);
}
public String getLabel()
{
return this.label;
}
...
}
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ActionWidget">
<attr name="label" format="string" />
<attr name="image" format="integer" />
</declare-styleable>
</resources>
Usage:
<com.someapp.form.ActionWidget
android:id="@+id/actionWidget1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:label="Text changed">
</com.someapp.form.ActionWidget>
When I launch the application, the text is still "TextView".
What should I do to make it load from the xml?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重写你的构造函数如下:
Rewrite your constructor as follows: