实现手动切换主题效果
1.第一步,配置实现 attr
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="text_color" format="reference"></attr>
<attr name="text_background" format="reference"></attr>
<attr name="fragment_left_background" format="reference"></attr>
<attr name="fragment_main_background" format="reference"></attr>
</resources>
2.第二部,创建color
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<!--/********************* basic *********************/-->
<color name="text_color_basic">#000</color>
<color name="text_background_basic">#fff</color>
<color name="fragment_background_basic">#FF4081</color>
<color name="fragmnet_main_background_basic">#49990000</color>
<!--/********************* dark *********************/-->
<color name="text_background_dark">#000</color>
<color name="fragmnet_main_background_dark">#0f0</color>
<color name="fragment_background_dark">#4081</color>
<color name="text_color_dark">#fff</color>
</resources>
3.创建stytle
<style name="basicTheme" parent="AppTheme">
<item name="text_color">@color/text_color_basic</item>
<item name="text_background">@color/text_background_basic</item>
<item name="fragment_left_background">@color/fragment_background_basic</item>
<item name="fragment_main_background">@color/fragmnet_main_background_basic</item>
</style>
<style name="darkTheme" parent="AppTheme">
<item name="text_color">@color/text_color_dark</item>
<item name="text_background">@color/text_background_dark</item>
<item name="fragment_left_background">@color/fragment_background_dark</item>
<item name="fragment_main_background">@color/fragmnet_main_background_dark</item>
</style>
4.在清单文件中设置主题是basicTheme主题
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oblivion.changetheme">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/darkTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5.布局文件使用?attr@color动态设定颜色
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fl_left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/fragment_left_background"></FrameLayout>
<FrameLayout
android:id="@+id/fl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/fragment_main_background">
<Button
android:id="@+id/bt_theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/text_background"
android:gravity="center"
android:text="修改主题"
android:textColor="?attr/text_color" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
6.在Main方法中进行设置
package com.oblivion.changetheme;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.oblivion.changetheme.utils.PrefUtils;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean theme = PrefUtils.getBoolean(getApplicationContext(), "theme", true);
//进入之前设置主题
if (theme) {
setTheme(R.style.basicTheme);
} else {
setTheme(R.style.darkTheme);
}
PrefUtils.putBoolean(getApplicationContext(), "theme", theme);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
initActionBar();
}
/**
* 初始化ActionBar
*/
private void initActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.emo_im_tongue_sticking_out);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);//显示图标
actionBar.setTitle("修改主题");
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
drawerToggle.syncState();//同步
drawerLayout.setDrawerListener(drawerToggle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerToggle.onOptionsItemSelected(item);
break;
case R.id.ss:
boolean theme = PrefUtils.getBoolean(getApplicationContext(), "theme", true);
if (!theme) {
setTheme(R.style.basicTheme);
} else {
setTheme(R.style.darkTheme);
}
// recreate();//重建Activity
Intent intent = getIntent();//这种方法不会产生黑屏效果
overridePendingTransition(0, 0);//不设置进入退出动画
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
PrefUtils.putBoolean(getApplicationContext(), "theme", !theme);
Toast.makeText(MainActivity.this, "" + theme, Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}