您的位置:首页 > 移动开发 > Android开发

Android主题更换简单实践

2016-03-29 23:02 519 查看

前言

  很多优秀的APP都支持多种主题,最常见的是白天和夜间模式。因此本篇博客是作者实践更换主题的记录,写出来与大家分享。

  实现更换主图这一功能有很多种方式,最简单的方式,就是定义多套主题。至于style文件当中,也适合新手学习与实践(大神勿喷)。言归正传,进入主题。

定义需要在各种主题下切换的属性

  这部分内容放在values下的attrs.xml文件中,这里我简单定义了几个如下:

<resources>

<attr name="rootViewBg" format="reference|color"/>
<attr name="titleBg" format="color"/>
<attr name="buttonBg" format="color"/>
<attr name="textColor" format="reference|color"/>
<attr name="buttonTextColor" format="color"/>

</resources>


定义多种主题

  既然是主题,自然是放在style.xml文件当中,这里以我的style文件为例,大家参考下,重点看注释里的日间模式和夜间模式:

<resources>

<!-- 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 name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>

<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>
<!--日间模式-->
<style name="Theme_Day" parent="AppTheme">
<item name="rootViewBg">@color/white</item>
<item name="textColor">@color/black_overlay</item>
<item name="buttonBg">#383838</item>
<item name="buttonTextColor">#008888</item>
</style>
<!--夜间模式-->
<style name="Theme_Night" parent="AppTheme">
<item name="rootViewBg">@color/black_overlay</item>
<item name="textColor">@color/white</item>
<item name="buttonBg">#000090</item>
<item name="buttonTextColor">#008811</item>
</style>
</resources>


将属性应用到布局文件当中

  布局文件比较简单,就是将我们自己定义的一些属性,应用到我们需要在不同主题下需要变更的View属性,注意代码当中注释的地方,比较简单,不做太多的讲解。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/rootViewBg">
<!--注意这里哦-->

<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="修改主题" />

<view.CircleProgress
android:id="@+id/circle_progress_welcome"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="@string/wasu_phone_tv"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif"
android:textColor="?attr/textColor"/>
<!--注意这里哦-->

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="@string/verion_info"
android:textColor="@color/white" />

</RelativeLayout>


AndroidManifest.xml文件中,应用默认的自定义主题

  先看我的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wasu.winton.phonetv">

<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="activity.WelcomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/Theme_Night">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="activity.LoginActivity"
/>
<activity android:name="activity.IndexActivity"/>
</application>

</manifest>


  注意一下我的第一个Activity中
android:theme
的值为我自定义的主题,为什么呢?因为只有自定义主题中,才包含了我自定义的属性呀,如果使用
android:theme:@style/AppTheme
会报运行时异常的,因为很多网上的例子,都没说这一点,我这里浪费了好几个小时。

  报出的异常:android.view.InflateException

android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout


Activity中切换主题

  主要就是这两个方法,这两个方法都是放在Activity的onCreate()方法中。通过将使用哪种主题写入SharePerfence文件,保证重新创建Activity可以生效。

public void initView() {
if(PerferenceUtil.getSp().getBoolean("dark",false)){
//配置文件中读取
setTheme(R.style.Theme_Day);
}else {
setTheme(R.style.Theme_Night);
}
setContentView(R.layout.activity_welcome);
circleProgress =(CircleProgress)findViewById(R.id.circle_progress_welcome);
circleProgress.startAnim();
bt = (Button)findViewById(R.id.bt);

}

@Override
public void initListener() {
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean theme = PerferenceUtil.getSp().getBoolean("dark", false);
PerferenceUtil.getEditor().putBoolean("dark",!theme).commit();//更换配置文件中主题
WelcomeActivity.this.recreate();//重新创建Activity
}
});
}


效果

  这种方式就是会出现闪现,怎么改善,后序研究。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: