ソースを参照

主题色设置

chendeben 1 年間 前
コミット
f54876f4b1

+ 0 - 3
entry/src/main/ets/MyAbilityStage.ets

@@ -6,11 +6,8 @@ export default class MyAbilityStage extends AbilityStage {
   async onCreate(): Promise<void> {
     hilog.info(0x0000, 'Heanup', '%{public}s', 'Ability onCreate');
     try {
-      let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', '#FF4081');
-      AppStorage.setOrCreate('themeColor', themeColor);
       // 保存当前颜色模式到 AppStorage
       AppStorage.setOrCreate('currentColorMode', this.context.config.colorMode);
-      hilog.info(0x0000, 'Heanup', '当前 colorMode:'+this.context.config.colorMode);
       // 确保 themeMode 有初始值(0: 跟随系统)
       if (AppStorage.get('themeMode') === undefined) {
         AppStorage.setOrCreate('themeMode', 0);

+ 9 - 4
entry/src/main/ets/pages/MainIndex.ets

@@ -4,10 +4,10 @@ import { StreamContent } from '../view/StreamContent'
 import { window } from '@kit.ArkUI'
 import { common } from '@kit.AbilityKit'
 import { AvSessionController } from '../controller/AvSessionController'
-import { ToastUtil } from '@pura/harmony-utils'
+import { PreferencesUtil, ToastUtil } from '@pura/harmony-utils'
 import { Utility } from '../common/util/Utility'
 import { IjkMediaPlayer } from '@ohos/ijkplayer'
-
+import { hilog } from '@kit.PerformanceAnalysisKit'
 @Entry
 @Component
 struct  MainIndex{
@@ -19,6 +19,7 @@ struct  MainIndex{
 
   @State rootPath:string = getContext(this).filesDir
 
+  @StorageProp('themeColor') themeColor: string = PreferencesUtil.getStringSync('THEME_COLOR', '#FF4081');
 
   @Builder TabBarBuilder(index:number,selIcon:ResourceStr,normalIcon:ResourceStr,text:string){
     Column(){
@@ -27,7 +28,7 @@ struct  MainIndex{
       Text(text)
         .fontSize(11)
         .margin({top:4})
-        .fontColor(this.selectIndex == index ? $r('app.color.title_bar_bg'):$r('app.color.tab_bar_normal') )
+        .fontColor(this.selectIndex == index ? this.themeColor : $r('app.color.tab_bar_normal') )
     }
   }
 
@@ -53,6 +54,10 @@ struct  MainIndex{
 
     IjkMediaPlayer.getInstance().stop();
     IjkMediaPlayer.getInstance().release();
+    let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', '#FF4081');
+    hilog.info(0x0000, 'Heanup', '当前 themeColor:'+themeColor);
+    AppStorage.setOrCreate('themeColor', themeColor);
+    this.themeColor = themeColor;
   }
 
   onPageShow() {
@@ -68,7 +73,7 @@ struct  MainIndex{
           .width('100%')
           .height(2)
       }
-      .backgroundColor($r('app.color.title_bar_bg'))
+      .backgroundColor(this.themeColor)
       .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
       Tabs({
         barPosition:BarPosition.End,

+ 5 - 0
entry/src/main/ets/pages/NewIndex.ets

@@ -28,6 +28,7 @@ import { resourceManager } from '@kit.LocalizationKit';
 import { systemShare } from '@kit.ShareKit';
 import { uniformTypeDescriptor } from '@kit.ArkData';
 import { IBestIcon } from "@ibestservices/ibest-ui";
+import { hilog } from '@kit.PerformanceAnalysisKit';
 
 
 const TAG = 'NewIndex'; // 日志标签
@@ -175,6 +176,10 @@ struct NewIndex{
     this.versionName =  AppUtil.getVersionName();
   // await ImageKnife.getInstance().initFileCache(this.context, 256, 256 * 1024 * 1024)
     this.isShowSponsorship = Utility.isOpenTime()
+    let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', '#FF4081');
+    hilog.info(0x0000, 'Heanup', '当前 NewIndex themeColor:'+themeColor);
+    AppStorage.setOrCreate('themeColor', themeColor);
+    this.themeColor=themeColor
 
   }
 

+ 4 - 1
entry/src/main/ets/pages/SettingPage.ets

@@ -169,6 +169,9 @@ export struct SettingPage {
     // 只用 themeMode 控制主题
     this.themeMode = PreferencesUtil.getNumberSync(SettingPage.THEME_MODE, 0)
     this.applyThemeMode(this.themeMode)
+    let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', '#FF4081');
+    AppStorage.setOrCreate('themeColor', themeColor);
+    this.themeColor = themeColor;
   }
 
   // 监听颜色模式变化
@@ -306,7 +309,7 @@ export struct SettingPage {
                     .margin({ right: 8 })
                     .onClick(() => {
                       this.themeColor = color
-                      PreferencesUtil.put(SettingPage.THEME_COLOR_KEY, color)
+                      PreferencesUtil.putSync(SettingPage.THEME_COLOR_KEY, color)
                       AppStorage.setOrCreate('themeColor', color)
                     })
                 })