不再信網上流傳的方法了!!android建立組件的真正寫法!

http://blog.csdn.net/jincf2011/article/details/6344678 氣死我了 這貨博客坑死大爺了大爺弄了兩天各類毛病html

一看官網原來這玩意兒不是這麼寫的java

官網教程:
android

All of the view classes defined in the Android framework extendView. Your custom view can also extend View directly, or you can save time by extending one of the existing view subclasses, such as Button.app

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

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

Define Custom Attributes


To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:ide

  • Define custom attributes for your view in a <declare-styleable> resource element函數

  • Specify values for the attributes in your XML layout佈局

  • Retrieve attribute values at runtimeui

  • Apply the retrieved attribute values to your viewthis

This section discusses how to define custom attributes and specify their values. The next section deals with retrieving and applying the values at runtime.

To define custom attributes, add <declare-styleable> resources to your project. It's customary to put these resources into a res/values/attrs.xml file. Here's an example of an attrs.xml file:

<resources>
   
<declare-styleable name="PieChart">
       
<attr name="showText" format="boolean" />
       
<attr name="labelPosition" format="enum">
           
<enum name="left" value="0"/>
           
<enum name="right" value="1"/>
       
</attr>
   
</declare-styleable>
</resources>

This code declares two custom attributes, showText and labelPosition, that belong to a styleable entity namedPieChart. The name of the styleable entity is, by convention, the same name as the name of the class that defines the custom view. Although it's not strictly necessary to follow this convention, many popular code editors depend on this naming convention to provide statement completion.

Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to thehttp://schemas.android.com/apk/res/android namespace, they belong tohttp://schemas.android.com/apk/res/[your package name]. For example, here's how to use the attributes defined for PieChart:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 
<com.example.customviews.charting.PieChart
     
custom:showText="true"
     
custom:labelPosition="left" />
</LinearLayout>

In order to avoid having to repeat the long namespace URI, the sample uses an xmlns directive. This directive assigns the alias custom to the namespace http://schemas.android.com/apk/res/com.example.customviews. You can choose any alias you want for your namespace.

Notice the name of the XML tag that adds the custom view to the layout. It is the fully qualified name of the custom view class. If your view class is an inner class, you must further qualify it with the name of the view's outer class. further. For instance, the PieChart class has an inner class called PieView. To use the custom attributes from this class, you would use the tag com.example.customviews.charting.PieChart$PieView.

Apply Custom Attributes


When a view is created from an XML layout, all of the attributes in the XML tag are read from the resource bundle and passed into the view's constructor as an AttributeSet. Although it's possible to read values from the AttributeSet directly, doing so has some disadvantages:

  • Resource references within attribute values are not resolved

  • Styles are not applied

Instead, pass the AttributeSet to obtainStyledAttributes(). This method passes back a TypedArray array of values that have already been dereferenced and styled.

The Android resource compiler does a lot of work for you to make calling obtainStyledAttributes() easier. For each <declare-styleable> resource in the res directory, the generated R.java defines both an array of attribute ids and a set of constants that define the index for each attribute in the array. You use the predefined constants to read the attributes from the TypedArray. Here's how the PieChart class reads its attributes:

public PieChart(Context context, AttributeSet attrs) {
   
super(context, attrs);
   
TypedArray a = context.getTheme().obtainStyledAttributes(
        attrs
,
        R
.styleable.PieChart,
       
0, 0);

   
try {
       mShowText
= a.getBoolean(R.styleable.PieChart_showText, false);
       mTextPos
= a.getInteger(R.styleable.PieChart_labelPosition, 0);
   
} finally {
       a
.recycle();
   
}
}

Note that TypedArray objects are a shared resource and must be recycled after use.

太長了是吧 簡單說一下:

自定義組件NewView繼承一個View類

重寫構造函數

 public NewView(Context context) {
        this(context,null);
    }
 public NewView(Context context, AttributeSet attrs) {
        super(context, attrs);
}

爲了專業一點 讓xml文件能配置這個組件 ,須要在values下的attrs.xml中定義一下自定義組件的參數

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NewView">
        <attr name="pressedIcon" format="reference" />
        <attr name="buttonIcon" format="reference"/>
        <attr name="text" format="string"/>
        <attr name="iconSize" format="dimension"/>
        <attr name="textSize" format="dimension"/>
    </declare-styleable>
</resources>

reference 表明引用資源文件 string表明字符 color 表明顏色  dimension表明大小

好了如今能夠在佈局文件中引入這個組件了

        <com.xxx.app.view.NewView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:text="@string/main_one"
            app:iconSize="20dp"/>

app表明屬性的namespace,若是用android studio的話

根元素加上

    xmlns:app="http://schemas.android.com/apk/res-auto"

若是是eclipse 須要替換成

xmlns:app="http://schemas.android.com/apk/res/包名"

而後咱們在構造函數中處理這些傳入的參數

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NewView, 0, 0);
    try{
       CharSequence text = a.getText(R.styleable.NewView_text);
    }finally{
    a.recycle();
    }

取到的參數怎麼處理 就不用說了哈

相關文章
相關標籤/搜索