1-Android开发知识安卓Android知识

Android样式和主题(一):简介

2017-05-04  本文已影响258人  08_carmelo

前言

样式Style和主题Theme是Android中很重要两个概念,用的时候很容易弄混淆,此篇我准备从自定义属性到自定义样式,了解一下Android里属性,样式和主题的原理。

简介

View的Style(样式)

假设给自定义View:MyView设定了几个自定义属性:attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="dml">
        <attr name="circleColor" format="color|reference"/>
        <attr name="circleSize" format="integer"/>
    </declare-styleable>
</resources>

然后把这几个属性封装成一个Style:style.xml

<resources>
    <style name="editStyle">
        <item name="circleColor">#343434</item>
        <item name="circleSize">40</item>
    </style>
</resources>

在布局文件中给MyView设置style:

style="@style/editStyle"

在MyView的构造方法中获取这些属性:

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.dml);
        int color = ta.getInt(R.styleable.dml_circleColor,-1);
        int size = ta.getInt(R.styleable.dml_circleSize,-1);
        Log.d("dml","color = " + color + "-------size = " + size);
    }

看下效果:
05-02 21:18:08.626 31441-31441/com.example.app2 D/dml: color = -13355980-------size = 40

Activity/Application的Theme(主题)

还是用上面自定义的两个属性,封装一个style,这次由于是用于Activity,那么需要额外的系统自带属性不然Activity不能正常工作,会崩溃

    <style name="AppEditStyle" parent="android:Theme.Material.Light">
        <item name="circleColor">#343434</item>
        <item name="circleSize">40</item>
    </style>

在menifest.xml的application节点定义:

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppEditStyle">
        <activity android:name=".StyleActivity">

然后在Activity中获取这两个属性:

public class StyleActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_style);
    }
    @Override
    protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
        super.onApplyThemeResource(theme, resid, first);
        TypedArray ta = theme.obtainStyledAttributes(R.styleable.dml);
        int color = ta.getInt(R.styleable.dml_circleColor,-1);
        int size = ta.getInt(R.styleable.dml_circleSize,-1);
        Log.d("dml","color = " + color + "-------size = " + size);
    }
}

看下效果:
05-02 21:38:15.285 19755-19755/com.example.app2 D/dml: color = -13355980-------size = 40

    @Override
    protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
            boolean first) {
        if (mParent == null) {
            super.onApplyThemeResource(theme, resid, first);
        } else {
            try {
                theme.setTo(mParent.getTheme());
            } catch (Exception e) {
                // Empty
            }
            theme.applyStyle(resid, false);
        }

        // Get the primary color and update the TaskDescription for this activity
        if (theme != null) {
            TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
            int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
            a.recycle();
            if (colorPrimary != 0) {
                ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null,
                        colorPrimary);
                setTaskDescription(v);
            }
        }
    }

下一篇:介绍下Android系统有哪些常见的主题

上一篇 下一篇

猜你喜欢

热点阅读