自定义attrs.xml与styles.xml还有Theme(主题)
2016-06-01 12:44
423 查看
在学习之前提个小知识: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, 10, 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" />
===================================================================================================================
以下为自定义Theme:在styles.xml文件下定义
<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>
相关文章推荐
- Win7用命令设置wifi热点的方法
- input输入框实时监控 并实现延迟发起请求
- Gradle配置自定义APK名称
- iOS控件之UITextField
- jQuery简单实现上下,左右滑动的方法
- 第二阶段团队冲刺站立会议08
- C#操作Access类
- 基于linux下的变量声明declare的用法
- MarkDownpad的使用
- UI适配攻略!安卓&苹果碎片化
- linux常用命令
- [JZOJ 3427] 归途与征程
- js动态函数和匿名函数
- Gradle配置keystore
- 资源管理器 C#文档 内涵资源链接
- 精通Objective-C 第一章~第三章读书笔记
- html绘制五角星。。
- 关于Linux网络程序的一些小问题
- win7虚拟无线工具+wifi下eclipse调试工具
- HTML5、formData移动浏览器上传图片