在学习之前提个小知识:Color.parseColor("#ffffff")这样可以转为0x开头形式的颜色值
在values文件夹下建立attrs.xml格式如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 一个自定义控件对应一个名称,此名称的属性不可以在别的控件中使用,注意:里面的基本属性也不可以和别的控件同名 --> <declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> <!-- 一个自定义控件对应一个名称,此名称的属性不可以在别的控件中使用,注意:里面的基本属性也不可以和别的控件同名 --> <declare-styleable name="MyButton"> <attr name="textColor1" format="color" /> <attr name="textSize1" format="dimension" /> </declare-styleable> </resources>
布局代码:注意自定义属性加上一句如下提示
<?xml version="1.0" encoding="utf-8"?> <!-- 自定义属性别忘了加入这句 xmlns:test="http://schemas.android.com/apk/res-auto" --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:test="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.k.myapplication.MyButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" test:textSize1="90px" test:textColor1="0x00000000" /> <com.example.k.myapplication.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" test:textSize="20px" test:textColor="#ffffff" /> </LinearLayout>自定义控件java代码:一目了然
package com.example.k.myapplication; import android.content.res.TypedArray; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class MyView extends View { private Paint mPaint; private static final String mString = "Welcome to Mr Wei's blog"; public MyView(Context context) { super(context); mPaint = new Paint(); } public MyView(Context context,AttributeSet attrs) { super(context,attrs); mPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF); float textSize = a.getDimension(R.styleable.MyView_textSize,36); mPaint.setTextSize(textSize); mPaint.setColor(textColor); a.recycle(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // 设置填充 mPaint.setStyle(Style.FILL); // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 canvas.drawRect(new Rect(10,10,100,100),mPaint); mPaint.setColor(Color.BLUE); // 绘制文字 canvas.drawText(mString,110,mPaint); } }
=================================================================================================================
定义自己的style,在styles.xml文件下定义:
<resources> <!-- 系统自带的Style --> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- 自定义的Style --> <!-- 布局文件的属性权重大于Style文件的属性,当布局文件控件没有定义android:text属性,Style文件的才会生效 --> <style name="MyStyle"> <item name="android:text">hello</item> </style> </resources>
在布局引用即可:
<com.example.k.myapplication.MyButton android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyStyle" android:id="@+id/button" test:textSize1="90px" />
===================================================================================================================
<style name="mytheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style>
然后在AndroidManifest.xml文件下引用,可以作用于整个应用,也可以作用于一个活动:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/mytheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>