標籤:android style blog http io color ar 使用 for
1、寫一個類繼承View或你想擴充功能的控制項(比如TextView)。
public class CustomView extends View { };
2、在/res/value下建立一個attr.xml檔案。
沒有這個檔案自訂控制項照樣能顯示出來,但只能使用所繼承的父類中包含的屬性,有了這個檔案可以增加自訂的命名空間,來設定自訂的屬性(其中format可選值見文尾)(大家看到這裡可能覺得很突兀,堅持看完,相信你會明白這裡的!)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
2、CustomView中重寫父類的構造方法(我一般把三個都寫上)在構造方法中擷取到自訂是屬性的值。
public CustomView(Context context) {
super(context);
}
//xml檔案解析的時候,會把標籤解析成一個類,標籤裡的屬性及屬性值都傳遞到AttributeSet裡了,所以我們要從這裡把屬性值擷取出來,設定給畫筆。
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView);//擷取定義在attr中屬性的屬性值,這個屬性值是在,把控制項設定到介面上時所使用的layout中設定的
int color = array.getColor(R.styleable.CustomView_textColor, Color.RED);//第二個參數為預設值
float size = array.getDimension(R.styleable.CustomView_textSize, 10);
paint = new Paint();
paint.setColor(color);
paint.setTextSize(size);
array.recycle();//必須有!!清空原array,防止以後出現原來設定的屬性。
}
4、重寫onDraw()方法。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("我是被畫出來的", 10, 100, paint);//第二個和第三個參數為座標的X軸Y軸
}
5、建立一個Activity(此處不再給出),在其使用的布局檔案中添加自訂控制項,並且可以引入自訂的命名空間,使用attr中定義的屬性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dinglin="http://schemas.android.com/apk/res/cn.itheima.customview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 使用自訂控制項要用包名+類名的形式,其中命名控制項dl中可以使用在attr.xml中定義的屬性 -->
<cn.itheima.customview.CustomView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
dl:textColor="#ff00ff00"
dl:textSize="25dip" >
</cn.itheima.customview.CustomView>
</LinearLayout>
搞定!運行就可以看到自己定義的控制項了,這個控制項有些醜,文章開頭已經說了,只是簡單介紹自訂控制項的開發流程!勿怪!
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Android自訂屬性時format選項可以取用的值
1. reference:參考某一資源ID。
(1)屬性定義:
<declare-styleable name="名稱">
<attr format="reference" name="background" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID" />
2. color:顏色值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr format="color" name="textColor" />
</declare-styleable>
(2)屬性使用:
<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00" />
3. boolean:布爾值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr format="boolean" name="focusable" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true" />
4. dimension:尺寸值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr format="dimension" name="layout_width" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width="42dip"
android:layout_height="42dip" />
5. float:浮點值。
(1)屬性定義:
<declare-styleable name="AlphaAnimation">
<attr format="float" name="fromAlpha" />
<attr format="float" name="toAlpha" />
</declare-styleable>
(2)屬性使用:
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7" />
6. integer:整型值。
(1)屬性定義:
<declare-styleable name="AnimatedRotateDrawable">
<attr format="integer" name="frameDuration" />
<attr format="integer" name="framesCount" />
</declare-styleable>
(2)屬性使用:
[html] view plaincopyprint?
<animated-rotate
android:frameDuration="100"
android:framesCount="12"/>
7. string:字串。
(1)屬性定義:
<declare-styleable name="MapView">
<attr format="string" name="apiKey" />
</declare-styleable>
(2)屬性使用:
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
8. fraction:百分數。
(1)屬性定義:
<declare-styleable name="RotateDrawable">
<attr format="fraction" name="pivotX" />
<attr format="fraction" name="pivotY" />
</declare-styleable>
(2)屬性使用:
<rotate
android:pivotX="200%"
android:pivotY="300%"/>
9. enum:枚舉值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)屬性使用:
<LinearLayout
android:orientation="vertical" >
</LinearLayout>
10. flag:位或運算。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="0" />
<flag name="stateUnchanged" value="1" />
<flag name="stateHidden" value="2" />
<flag name="stateAlwaysHidden" value="3" />
<flag name="stateVisible" value="4" />
<flag name="stateAlwaysVisible" value="5" />
<flag name="adjustUnspecified" value="0x00" />
<flag name="adjustResize" value="0x10" />
<flag name="adjustPan" value="0x20" />
<flag name="adjustNothing" value="0x30" />
</attr>
</declare-styleable>
(2)屬性使用:
<activity
android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >
</activity>
注意:屬性定義時可以指定多種類型值:
(1)屬性定義:
<declare-styleable name="名稱">
<attr format="reference|color" name="background" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID|#00FF00" />
轉自:http://blog.csdn.net/dinglin_87/article/details/7431545
android 自訂控制項步驟