增加电量显示 3289/2
authorxue <2144439972@qq.com>
Wed, 18 Mar 2026 11:44:51 +0000 (19:44 +0800)
committerxue <2144439972@qq.com>
Wed, 18 Mar 2026 11:44:51 +0000 (19:44 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/main/include/PullUpDownView.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/main/src/MainPresenterSample.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/main/src/PullUpDownView.cpp

index 9732fd5..e2e6d61 100644 (file)
@@ -87,6 +87,7 @@ namespace OHOS
         UILabel *GetDateAndWeekLabel(void);
         void UpdateSignalStrengthStatusAndBatteryPosition();
         void UpdateBatteryProcess();
+        UILabel *GetBatteryPercentLabel(void);
 
     private:
         void InitBaseData();
@@ -102,6 +103,7 @@ namespace OHOS
         UIViewGroup *group1{nullptr};
         UIImageView *batteryimage{nullptr};          // 电池图片
         UIBoxProgress *progress{nullptr};            // 电池进度条
+        UILabel *batteryPercentLabel{nullptr};       // 电量百分比文本
         UILabel *showDateAndWeekLabel{nullptr};      // 显示日期和星期
         UIImageView *chargeBatteryicon{nullptr};     //充电图标
         bool isChargeBatteryIconVisible{false};     // 充电图标是否可见
index fb1192c..55f9c9e 100644 (file)
          #endif
          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "battery_get_voltage = %d", value);
          std::string dateAndweek = std::string(dateStr) + " " + std::string(weekStr);
-         if (PullUpDownView::GetInstance() != nullptr)
-         {
-             PullUpDownView::GetInstance()->GetProgress()->SetValue(value);
-             PullUpDownView::GetInstance()->GetDateAndWeekLabel()->SetText(dateAndweek.c_str());
+        if (PullUpDownView::GetInstance() != nullptr)
+        {
+            PullUpDownView::GetInstance()->GetProgress()->SetValue(value);
+            PullUpDownView::GetInstance()->GetDateAndWeekLabel()->SetText(dateAndweek.c_str());
+            UILabel *batLabel = PullUpDownView::GetInstance()->GetBatteryPercentLabel();
+            if (batLabel != nullptr)
+            {
+                std::string percentStr = std::to_string(static_cast<int>(value)) + "%";
+                batLabel->SetText(percentStr.c_str());
+            }
          }
  
          // 释放动态分配的内存
index 6162ebe..1f37548 100644 (file)
@@ -272,13 +272,15 @@ namespace OHOS
         group1->SetStyle(STYLE_BACKGROUND_OPA, 0);
         group[DROPDOWNVIEW]->Add(group1);
         /* ----------------------------------- 电池 ----------------------------------- */
+        const int BATTERY_LEFT_OFFSET = 30;  /* 电池图标整体左移 */
+        int batteryAreaWidth = 48; /* 图标+间距+电量文本 */
         batteryimage = new UIImageView();
         if (batteryimage == nullptr)
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryimage new fail");
             return;
         }
-        batteryimage->SetPosition((group1->GetWidth() - 48) / 2, (group1->GetHeight() - 30) / 2, 48, 30);
+        batteryimage->SetPosition((group1->GetWidth() - batteryAreaWidth) / 2 - BATTERY_LEFT_OFFSET, (group1->GetHeight() - 30) / 2, 48, 30);
         std::string batteryimagesPath = "/user/res/Battery/Battery_White.bin";
         batteryimage->SetSrc(batteryimagesPath.c_str());
         group1->Add(batteryimage);
@@ -313,6 +315,22 @@ namespace OHOS
         chargeBatteryicon->SetResizeMode(UIImageView::CONTAIN);
         chargeBatteryicon->SetVisible(false);
         group1->Add(chargeBatteryicon);
+        /* ----------------------------------- 电量百分比文本 ----------------------------------- */
+        batteryPercentLabel = new UILabel();
+        if (batteryPercentLabel == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryPercentLabel new fail");
+            return;
+        }
+        batteryPercentLabel->SetPosition(batteryimage->GetX() + 48 + 8, (group1->GetHeight() - 28) / 2, 50, 28);
+        batteryPercentLabel->SetStyle(STYLE_TEXT_FONT, 24);
+        batteryPercentLabel->SetStyle(STYLE_LINE_HEIGHT, 28);
+        batteryPercentLabel->SetTextColor(Color::White());
+        batteryPercentLabel->SetText("0%");
+        batteryPercentLabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
+        batteryPercentLabel->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
+        batteryPercentLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, 26);
+        group1->Add(batteryPercentLabel);
         /* --------------------------------- 显示日期和星期 -------------------------------- */
         showDateAndWeekLabel = new UILabel();
         if (showDateAndWeekLabel == nullptr)
@@ -353,6 +371,11 @@ namespace OHOS
                 delete chargeBatteryicon;
                 chargeBatteryicon = nullptr;
             }
+            if (batteryPercentLabel != nullptr)
+            {
+                delete batteryPercentLabel;
+                batteryPercentLabel = nullptr;
+            }
             delete group1;
             group1 = nullptr;
         }
@@ -415,6 +438,11 @@ namespace OHOS
     {
         return showDateAndWeekLabel;
     }
+
+    UILabel *PullUpDownView::GetBatteryPercentLabel(void)
+    {
+        return batteryPercentLabel;
+    }
     /* -------------------------------------------------------------------------- */
     AppGroupView *PullUpDownView::DropDownCardPage1Init(void)
     {