• android如何切换皮肤


    1、先定义attr文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr name="layout_bg" format="reference"/>
        <attr name="layout_item_bg" format="reference"/>
        <attr name="textColor" format="reference"/>
    </resources>

    2、定义实际的属性值文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="AppTheme.Default">
            <item name="layout_bg">@color/left_draw_bg</item>
            <item name="layout_item_bg">@color/window_background</item>
            <item name="textColor">@color/gray</item>
        </style>
    
        <style name="AppTheme.Green">
            <item name="layout_bg">@color/left_draw_bg</item>
            <item name="layout_item_bg">@color/window_background</item>
            <item name="textColor">@color/green</item>
        </style>
    
        <style name="AppTheme.Red">
            <item name="layout_bg">@color/left_draw_bg</item>
            <item name="layout_item_bg">@color/window_background</item>
            <item name="textColor">@color/red</item>
        </style>
    
    </resources>

    3、使用我们自定义的属性

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/layout_bg"
        android:orientation="vertical">
    <TextView
                    android:id="@+id/tv_left_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="@dimen/s_117"
                    android:layout_marginTop="@dimen/s_117"
                    android:textColor="?attr/textColor"
                    android:text="@string/app_name" />

    通过?attr/+自定义属性名引用

    4、Activity调用相应的主题

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTheme(R.style.AppTheme_Green);
            setContentView(R.layout.activity_main);
            initView();
        }

    可见android系统,让人先简单的使用主题,只要调用setTheme就可以搞定。

    系统将AppTheme.Green改成了AppTheme_Green来调用。

  • 相关阅读:
    密码保护
    实现搜索功能
    完成个人中心—导航标签
    个人中心标签页导航
    评论列表显示及排序,个人中心显示
    完成评论功能
    从首页问答标题到问答详情页
    首页列表显示全部问答,完成问答详情页布局。
    JavaScript Array Reduce用于数组求和
    【Angular5】 返回前一页面 go back to previous page
  • 原文地址:https://www.cnblogs.com/jiduoduo/p/5238514.html
Copyright © 2020-2023  润新知