MyActivity.java
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.myreactdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import com.myreactdemo.util.ThemeUtile;
/**
* 1、 原生页面与RN 跳转交互
* 2、 黑白主题切换
*/
public class MyActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ThemeUtile.changeTheme(this);
setContentView(createView());
}
private View createView() {
LinearLayout ll= new LinearLayout(this);
ll.setGravity(Gravity.CENTER);
ll.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// 设置文字
TextView mTextView = new TextView(this);
mTextView.setText("hello world");
// TypedValue typedValue = new TypedValue();
// getTheme().resolveAttribute(R.attr.Text_bg_Color, typedValue, true);
// mTextView.setBackgroundColor(typedValue.resourceId);
//设置按钮
Button mButton =new Button(this);
mButton.setText(R.string.change_theme);
mButton.setOnClickListener(this);
LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 在父类布局中添加它,及布局样式
ll.addView(mTextView, mLayoutParams);
ll.addView(mButton, mLayoutParams);
return ll;
}
@Override
public void onClick(View v) {
// if (ThemeUtile.night) {
// ThemeUtile.night = false;
// } else {
// ThemeUtile.night = true;
// }
// recreate();
//name相当于一个key,content是返回的内容
//结束当前页面
finish();
//出现问题:由于在当前屏幕值重新设置主题,会导致重新调用onCreate()方法导致闪屏
/**
* 解决方案1:
* 可以在一个新开的Activity中通过ThemeUtile.night变量重新设置主题,但不调用recreate()方法切换主题,
* 而是“手动”设置需要改变的属性,在退出该Activity时,使用Intent回到之前的界面,
* 并 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
* ,使之前的Activity全部出栈,重新创建一个新的Activity,执行onCreate()方法,从而改变主题。
* 虽然不是很优雅,但是完成了不闪屏切换Android App主题。
*/
}
}