The application package is "test.animation"
Attrs
/YOUR_APP/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ButtonView">
<attr name="hello_a" format="color" />
<attr name="hello_b" format="dimension" />
</declare-styleable>
</resources>
/YOUR_APP/res/layout/main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/test.animation"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
<test.animation.ButtonView
android:id="@+id/btnD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:hello_a="@color/red"
custom:hello_b="@dimen/wordsize"
android:text="@string/btn_d" />
...
</ScrollView>
/YOUR_APP/src/test/animatioin/ButtonView
package test.animation;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.Button;
public class ButtonView extends Button {
private Paint myPaint;
public ButtonView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ButtonView(Context context) {
super(context);
}
public ButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
myPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ButtonView);
float textSize = a.getDimension(R.styleable.ButtonView_hello_b, 30);
int textColor = a.getColor(R.styleable.ButtonView_hello_a, 0xFFFFFFFF);
myPaint.setColor(textColor);
myPaint.setTextSize(textSize);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//TODO: Apply myPaint to draw the view.
}
}
Style
/YOUR_APP/res/values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
...
<style name="test_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
/YOUR_APP/res/layout/main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/test.animation"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
<Button
android:id="@+id/btnC"
style="@style/test_style"
android:text="@string/btn_c" />
...
</ScrollView>
<?xml version="1.0" encoding="utf-8"?><resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style></resources>
themes.xml
<item name="windowBackground">@android:drawable/screen_background_dark</item>
<item name="windowFrame">@null</item>
<item name="windowNoTitle">false</item>
<item name="windowFullscreen">false</item>
<item name="windowIsFloating">false</item>
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
<item name="windowTitleStyle">@android:style/WindowTitle</item>
<item name="windowTitleSize">25dip</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.linear_layout_3);
setTheme should be in the front of setContentView.