开发者

Eclipse do not show my custom attributes (attrs.xml)

Using Eclipse, I created a simple Android project containing a custom component (MyTextView, inherited from android.widget.TextView). After I created the file, attrs.xml where I added an attribute to this class, as specified below:

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

I added an object in my main.xml linear layout:

<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>

When I open the main.xml, and choose the tab 'Graphical Layout', selecting my component, the custom attribute does not appear in the tab 'Properties' of the Eclipse, but the original attributes of class inheritance (android.widget.TextView) appears.

This is a mistake? What is the p开发者_如何转开发rocedure to submit my custom attributes? Or I'm mistaken?

Thank you. Luciano.


I just restarted Eclipse and it worked! :D


To allow the Android Developer Tools to interact with your view, at a minimum you must provide a constructor that takes a Context and an AttributeSet object as parameters. This constructor allows the layout editor to create and edit an instance of your view.

class PieChart extends View 
{
    public PieChart(Context context, AttributeSet attrs)  
    {
        super(context, attrs);
    }
}

or if still not solve then You can do this in your Custom View:

if(!isInEditMode())
{
   // Your custom code that is not letting the Visual Editor draw properly
   // i.e. thread spawning or other things in the constructor
}

detail of isInEditMode

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜