AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybutton"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mybutton.MyButton"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
src/com/example/mybutton/MyButton.java
package com.example.mybutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MyButton extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_button);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_button, menu);
return true;
}
}
res/layout/activity_my_button.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:my="http://schemas.android.com/apk/res/com.example.mybutton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyButton" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.example.mybutton.WindButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
my:myTextSize="30sp"
my:myTextColor="#ff6666"
android:textColor="@android:color/black"
android:text="Button" />
</RelativeLayout>
解释:xmlns: my="http://schemas.android.com/apk/res/ com.example.mybutton"
my:属性前缀
com.example.mybutton:包名
src/com/example/mybutton/WindButton.java
package com.example.mybutton;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
public class WindButton extends Button{
private static final String TAG = "WindButton";
public WindButton(Context context, AttributeSet attrs) {
super(context, attrs);
Log.v(TAG, "WindButton 2 pares"); //声明在xml的类会调用这个构造函数
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.myView);//TypedArray是一个数组容器
float textSize = a.getDimension(R.styleable.myView_myTextSize, 30);//防止在XML文件里没有定义,就加上了默认值30
int textColor = a.getColor(R.styleable.myView_myTextColor, 0xFFFFFFFF);//同上,这里的属性是:名字_属性名
setTextSize(textSize);
setTextColor(textColor);
setBackgroundResource(R.drawable.ic_launcher);
a.recycle(); //TypedArray类型不会自动回收,为避免影响下次使用,好的习惯是主动回收
}
public WindButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Log.v(TAG, "WindButton 3 pares");
// TODO Auto-generated constructor stub
}
public WindButton(Context context) {
super(context);
Log.v(TAG, "WindButton 1 pares");
// TODO Auto-generated constructor stub
}
}