自定义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, 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值