使用Eclipse,我创建了一个简单的Android项目,其中包含一个自定义组件( android.widget.TextView).继承的MyTextView)在创建该文件后,attrs.xml向该类添加了一个属性,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView" >
<attr name="AttributeOne" format="integer"/>
</declare-styleable>
</resources>我在我的main.xml线性布局中添加了一个对象:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mt="http://schemas.android.com/apk/res/myprogram.custom"
android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<myprogram.custom.MyTextView
android:text="MyTextView"
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</myprogram.custom.MyTextView>
</LinearLayout>当我打开main.xml,选择选项卡‘图形布局’,选择我的组件时,自定义属性不会出现在Eclipse的选项卡'Properties‘中,但是类继承的原始属性会出现。
这是个错误吗?提交自定义属性的过程是什么?还是我弄错了?
谢谢。卢西亚诺。
发布于 2013-05-31 11:40:47
我刚刚重新启动了Eclipse,它成功了!:D
发布于 2015-01-04 10:35:02
要允许Android开发工具与视图交互,至少必须提供一个构造函数,该构造函数以上下文和AttributeSet对象作为参数。此构造函数允许布局编辑器创建和编辑视图的实例。
class PieChart extends View
{
public PieChart(Context context, AttributeSet attrs)
{
super(context, attrs);
}
}如果仍未解决,则可以在自定义视图中这样做:
if(!isInEditMode())
{
// Your custom code that is not letting the Visual Editor draw properly
// i.e. thread spawning or other things in the constructor
}isInEditMode的细节
https://stackoverflow.com/questions/6575912
复制相似问题