Skip to content

Commit

Permalink
更改README
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Jun 5, 2017
1 parent d0e2826 commit 3f04b8e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 22 deletions.
106 changes: 84 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
# AndroidChangeSkinDemo

夜间模式的测试Demo,通过Theme实现(attrs.xml+styles.xml+Activity.setTheme())
夜间模式Demo,通过Theme实现(attrs.xml+styles.xml+Activity.setTheme())

## 效果展示:
![](https://github.com/maning0303/AndroidChangeSkinDemo/raw/master/screenshots/001.gif)

## 如何添加
### Gradle添加:
#### 1.在Project的build.gradle中添加仓库地址

``` gradle
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
```

#### 2.在Module目录下的build.gradle中添加依赖
``` gradle
dependencies {
compile 'com.github.maning0303:MNChangeSkin:V1.0.0'
}
```


## 实现步骤:

##### 1.自定义属性
```java
<attr name="app_background" format="reference|color" />
Expand All @@ -27,41 +52,78 @@

```

#### 注意(具体的看代码)
#### 代码设置
```java

1.最好设置一个BaseActivity,继承它:
1.Application的onCreate() 中初始化:
//设置白天主题的和夜间主题
SkinManager.setThemeID(R.style.DayTheme, R.style.NightTheme);

2.新建一个BaseActivity,所有Activity继承它:
public class BaseActivity extends Activity {
//当前Activity的主题
public int skinBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
//设置主题
SkinManager.onActivityCreateSetSkin(this);
super.onCreate(savedInstanceState);
//获取当前的主题
skinBase = SkinManager.getCurrentSkinType(this);
}
}

2.需要实现夜间模式的控件都要在attr里面添加,然后在两种主题都写上才可以。

3.XML写不了的就手动获取当前主题,手动设置(部分ListView里面可能需要手动设置):
int currentSkinType = SkinManager.getCurrentSkinType(SettingActivity.this);

3.XML文件中设置不了的就获取当前主题,手动设置:
int currentSkinType = SkinManager.getCurrentSkinType(this);
if (SkinManager.THEME_DAY == currentSkinType) {
SkinManager.changeSkin(SettingActivity.this, SkinManager.THEME_NIGHT);
btn_checked.setBackColorRes(R.color.kswBackColor_night);
btn_checked.setThumbColorRes(R.color.kswThumbColor_night);
//改变颜色...
} else {
SkinManager.changeSkin(SettingActivity.this, SkinManager.THEME_DAY);
btn_checked.setBackColorRes(R.color.kswBackColor);
btn_checked.setThumbColorRes(R.color.kswThumbColor);
//改变颜色...
}

4.广播的作用---主要防止设置界面太深,而设置界面之前的页面改不了(更换主题必须重启Activity才能有效果),如果更改主题的按钮在首页面,就没有必要使用注册广播了。

```
public void registerSkinReceiver() {
skinBroadcastReceiver = new SkinBroadcastReceiver() {
@Override
public void onChangeSkin(int currentTheme) {
Log.i("onChangeSkin", "广播来了" + currentTheme);
//重启Activity
recreate();
}
};
SkinManager.registerSkinReceiver(OtherActivity.this, skinBroadcastReceiver);
}

public void unregisterSkinReceiver() {
if (skinBroadcastReceiver != null) {
SkinManager.unregisterSkinReceiver(this, skinBroadcastReceiver);
}
}

## 效果展示:
![](https://github.com/maning0303/AndroidChangeSkinDemo/raw/master/screenshots/001.gif)
5.改变主题:
//改变主题:自动根据当前主题改变白天和黑夜
SkinManager.changeSkin(this);
//改变主题:指定主题
//SkinManager.changeSkin(this,R.style.DayTheme);
//重启当前Activity
startActivity(new Intent(this, SettingActivity.class));
//重要:做一个自定义动画,避免出现闪烁现象
overridePendingTransition(com.maning.themelibrary.R.anim.mn_theme_activity_enter, com.maning.themelibrary.R.anim.mn_theme_activity_exit);
finish();


6.关于WebView设置夜间模式:
webView.setWebViewClient(new WebViewClient() {
...

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//设置webView,这里是设置夜间模式的颜色
String backgroudColor = "#232736";
String fontColor = "#626f9b";
String urlColor = "#9AACEC";
SkinManager.setColorWebView(webView, backgroudColor, fontColor, urlColor);
}

...
});


```
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void registerSkinReceiver() {
@Override
public void onChangeSkin(int currentTheme) {
Log.i("onChangeSkin", "OtherActivity广播来了" + currentTheme);
//重启Activity
recreate();
}
};
Expand Down

0 comments on commit 3f04b8e

Please sign in to comment.