自定义attrs.xml与styles.xml还有Theme主题

在学习之前提个小知识: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"
        />

===================================================================================================================

以下为自定义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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇