【问题标题】:Eclipse do not show my custom attributes (attrs.xml)Eclipse 不显示我的自定义属性 (attrs.xml)
【发布时间】:2011-07-04 20:43:06
【问题描述】:

使用 Eclipse,我创建了一个简单的 Android 项目,其中包含一个自定义组件(MyTextView,继承自 android.widget.TextView)。 创建文件后,我在 attrs.xml 中向此类添加了一个属性,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTextView" >
        <attr name="AttributeOne" format="integer"/>
    </declare-styleable>
</resources>

我在 ma​​in.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>

当我打开ma​​in.xml,选择'Graphical Layout'选项卡,选择我的组件,Eclipse的'Properties'选项卡中没有出现自定义属性,而是原来的类继承属性(android.widget.TextView)出现。
这是个错误?提交我的自定义属性的程序是什么? 还是我弄错了?

谢谢。 卢西亚诺。

【问题讨论】:

  • 您是否尝试关闭您的项目并再次打开它?我注意到,除非我重新加载项目,否则 Eclipse 中的图形布局似乎不会更新(添加主题时)的主题。
  • 是的,但还是不行……

标签: android xml eclipse custom-attributes


【解决方案1】:

我刚刚重新启动了 Eclipse,它成功了! :D

【讨论】:

  • 是的,多么好的解决方案!这意味着,当您认为您做得对时,它似乎是错误的,因此您可能会将代码重做无穷大,尽管您需要做的就是重新启动环境——这就是 Eclipse 的全部问题。您可以处理不存在的错误。
【解决方案2】:

要允许Android Developer Tools 与您的视图交互,您至少必须提供一个以ContextAttributeSet 对象作为参数的构造函数。此构造函数允许布局编辑器创建和编辑视图的实例。

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的详细信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2014-06-16
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多