Skip to content

Commit 88813b2

Browse files
committed
更新布局代码
1 parent ca0241d commit 88813b2

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/main/java/com/clock/study/activity/DayNightActivity.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.clock.study.activity;
22

3+
import android.content.Context;
34
import android.content.res.Resources;
45
import android.os.Bundle;
56
import android.support.v7.app.AppCompatActivity;
7+
import android.support.v7.view.ContextThemeWrapper;
68
import android.support.v7.widget.LinearLayoutManager;
79
import android.support.v7.widget.RecyclerView;
810
import android.util.Log;
@@ -19,6 +21,9 @@
1921
import com.clock.study.helper.DayNightHelper;
2022
import com.clock.study.type.DayNight;
2123

24+
import java.lang.reflect.Field;
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.lang.reflect.Method;
2227
import java.util.ArrayList;
2328
import java.util.List;
2429

@@ -125,9 +130,8 @@ private void changeThemeByJianShu() {
125130
Resources.Theme theme = getTheme();
126131

127132
theme.resolveAttribute(R.attr.clockBackground, typedValue, true);
128-
Log.i(TAG, "clockBackground resourceId: " + typedValue.resourceId);
129133
mHeaderLayout.setBackgroundResource(typedValue.resourceId);
130-
mRecyclerView.setBackgroundResource(typedValue.resourceId);
134+
//mRecyclerView.setBackgroundResource(typedValue.resourceId);
131135
for (RelativeLayout layout : mLayoutList) {
132136
layout.setBackgroundResource(typedValue.resourceId);
133137
}
@@ -139,9 +143,39 @@ private void changeThemeByJianShu() {
139143
}
140144

141145
theme.resolveAttribute(R.attr.clockTextColor, typedValue, true);
142-
Log.i(TAG, "clockTextColor resourceId: " + typedValue.resourceId);
146+
Resources resources = getResources();
143147
for (TextView textView : mTextViewList) {
144-
textView.setTextColor(getResources().getColor(typedValue.resourceId));
148+
textView.setTextColor(resources.getColor(typedValue.resourceId));
149+
}
150+
151+
Class<RecyclerView> recyclerViewClass = RecyclerView.class;
152+
try {
153+
Field declaredField = recyclerViewClass.getDeclaredField("mRecycler");
154+
declaredField.setAccessible(true);
155+
Method declaredMethod = Class.forName(RecyclerView.Recycler.class.getName()).getDeclaredMethod("clear", (Class<?>[]) new Class[0]);
156+
declaredMethod.setAccessible(true);
157+
declaredMethod.invoke(declaredField.get(mRecyclerView), new Object[0]);
158+
RecyclerView.RecycledViewPool recycledViewPool = mRecyclerView.getRecycledViewPool();
159+
recycledViewPool.clear();
160+
Context context = mRecyclerView.getContext();
161+
if (context instanceof ContextThemeWrapper) {
162+
if (mDayNightHelper.isDay()) {
163+
context.setTheme(R.style.DayTheme);
164+
} else {
165+
context.setTheme(R.style.NightTheme);
166+
}
167+
}
168+
169+
} catch (NoSuchFieldException e) {
170+
e.printStackTrace();
171+
} catch (ClassNotFoundException e) {
172+
e.printStackTrace();
173+
} catch (NoSuchMethodException e) {
174+
e.printStackTrace();
175+
} catch (InvocationTargetException e) {
176+
e.printStackTrace();
177+
} catch (IllegalAccessException e) {
178+
e.printStackTrace();
145179
}
146180
}
147181

src/main/res/layout/activity_day_night.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
android:layout_centerVertical="true"
2727
android:layout_marginLeft="10dp"
2828
android:background="?attr/clockBackground"
29-
android:text="简书切换方案" />
29+
android:text="简书切换方案"
30+
android:textColor="?attr/clockTextColor" />
3031

3132
<CheckBox
3233
android:id="@+id/ckb_jianshu"
@@ -50,7 +51,8 @@
5051
android:layout_centerVertical="true"
5152
android:layout_marginLeft="10dp"
5253
android:background="?attr/clockBackground"
53-
android:text="知乎切换方案" />
54+
android:text="知乎切换方案"
55+
android:textColor="?attr/clockTextColor" />
5456

5557
<CheckBox
5658
android:id="@+id/ckb_zhihu"

src/main/res/layout/author_info_layout.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="80dp"
6+
android:background="?attr/clockBackground"
67
android:orientation="horizontal">
78

89
<ImageView
@@ -16,22 +17,27 @@
1617
android:layout_width="match_parent"
1718
android:layout_height="match_parent"
1819
android:layout_margin="5dp"
20+
android:background="?attr/clockBackground"
1921
android:orientation="vertical">
2022

2123
<TextView
2224
android:id="@+id/tv_nickname"
2325
android:layout_width="wrap_content"
2426
android:layout_height="wrap_content"
2527
android:layout_marginTop="10dp"
28+
android:background="?attr/clockBackground"
29+
android:text="D_clock爱吃葱花"
30+
android:textColor="?attr/clockTextColor"
2631
android:textSize="20sp"
27-
android:textStyle="bold"
28-
android:text="D_clock爱吃葱花" />
32+
android:textStyle="bold" />
2933

3034
<TextView
3135
android:id="@+id/tv_motto"
3236
android:layout_width="wrap_content"
3337
android:layout_height="wrap_content"
3438
android:layout_marginTop="10dp"
35-
android:text="Diycode 大管家,闭关写代码" />
39+
android:background="?attr/clockBackground"
40+
android:text="Diycode 大管家,闭关写代码"
41+
android:textColor="?attr/clockTextColor" />
3642
</LinearLayout>
3743
</LinearLayout>

0 commit comments

Comments
 (0)