自定义attrs.xml与styles.xml还有Theme(主题)
来源:互联网 发布:linux查看arp表 编辑:程序博客网 时间:2024/06/06 03:31
在学习之前提个小知识: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>
0 0
- 自定义attrs.xml与styles.xml还有Theme(主题)
- 自定义控件 attrs.xml
- 安卓自定义theme以及attrs,styles资源文件详解(一)
- Android中自定义主题风格theme.xml
- attrs.xml styles.xml R.styleable.xxx 属性
- 利用attrs.xml和 styles.xml定义Preference
- Android 自定义属性 attrs.xml
- 【Android】自定义属性 attrs.xml
- Android 自定义属性 attrs.xml
- 使用attrs.xml自定义属性
- 自定义view圆环attrs.xml
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray)的使用
- html绘制五角星。。
- SDAU练习三总结
- Gradle配置keystore
- js动态函数和匿名函数
- UI适配攻略!安卓&苹果碎片化
- 自定义attrs.xml与styles.xml还有Theme(主题)
- MarkDownpad的使用
- [JZOJ3427]【NOIP2013模拟】归途与征程
- HDFS 文件权限
- iOS控件之UITextField
- Gradle配置自定义APK名称
- POJ 3252 Round Numbers(数学问题)
- input输入框实时监控 并实现延迟发起请求
- Win7用命令设置wifi热点的方法