本文主要是介绍安卓7.0上在状态栏上加电池百分比,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
》》》》》》
安卓7.0上的statusbar上是默认不带电池百分比的。需要我们加上去。有两个方法,先写第一个方法
1. 在frameworks/base/packages/SystemUI/res/layout/system_icons.xml中添加布局文件:
<TextView android:id="@+id/system_icon_battery_level"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginStart="@dimen/header_battery_margin_expanded"android:textColor="#ffffff"android:textSize="@dimen/battery_level_text_size"android:visibility="gone"android:importantForAccessibility="noHideDescendants"/>
2. 在frameworks/base/packages/SystemUI/res/values/strings.xml中添加字符串
<string name="battery_level_template"><xliff:g id="level" example="45">%d</xliff:g>%%</string>
3.在
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
mKeyguardStatusBar.setUserInfoController(mUserInfoController);mKeyguardStatusBar.setUserSwitcherController(mUserSwitcherController);mUserInfoController.reloadUserInfo();((BatteryMeterView) mStatusBarView.findViewById(R.id.battery)).setBatteryController(mBatteryController);mKeyguardStatusBar.setBatteryController(mBatteryController);///M: add{@if (true) {//show battery percentmStatusBarView.setBatteryController(mBatteryController);}///@}
4.在
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
首先引入两个包
import android.widget.TextView;
import com.android.systemui.statusbar.policy.BatteryController;
这个类需要实现接口
public class PhoneStatusBarView extends PanelBar implements BatteryController.BatteryStateChangeCallback
增加其他方法
public void onFinishInflate() {mBarTransitions.init();///M:add {@mBatteryLevel = (TextView) findViewById(R.id.system_icon_battery_level);///@}}
private TextView mBatteryLevel;private BatteryController mBatteryController;public void setBatteryController(BatteryController batteryController) {mBatteryController = batteryController;mBatteryController.addStateChangedCallback(this);if (mBatteryLevel != null){mBatteryLevel.setVisibility(View.VISIBLE);}}@Overridepublic void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {if (mBatteryLevel != null) {mBatteryLevel.setText(getResources().getString(R.string.battery_level_template, level));}}@Overridepublic void onPowerSaveChanged(boolean isPowerSave) {// could not care less}
第二种方法
diff --git a/packages/SystemUI/res/layout/status_bar.xml b/packages/SystemUI/res/layout/status_bar.xml
index 82119a3..455edf9 100644
--- a/packages/SystemUI/res/layout/status_bar.xml
+++ b/packages/SystemUI/res/layout/status_bar.xml
@@ -80,6 +80,15 @@android:textColor="#ffffff"android:textSize="@dimen/battery_level_text_size"android:layout_marginStart="7dp"/>
+ <TextView android:id="@+id/battery_level"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical"
+ android:layout_marginStart="@dimen/header_battery_margin_keyguard"
+ android:paddingEnd="@dimen/battery_level_padding_end"
+ android:textColor="#ffffff"
+ android:textSize="@dimen/battery_level_text_size"
+ android:importantForAccessibility="noHideDescendants"/><com.android.systemui.statusbar.policy.Clockandroid:id="@+id/clock"
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index f17403d..8bee44c 100755
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.javaimport android.app.ActivityManager.RunningTaskInfo;//add by yuhaixiang for factory end
+import com.android.systemui.FontSizeUtils;
+import java.text.NumberFormat;public class PhoneStatusBar extends BaseStatusBar implements DemoMode,DragDownHelper.DragDownCallback, ActivityStarter, OnUnlockMethodChangedListener,
@@ -443,6 +445,15 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,private int mNavigationIconHints = 0;private HandlerThread mHandlerThread;
+
+ private TextView mBatteryLevel;
+ private ContentObserver mBatteryLevelShow = new ContentObserver(new Handler()) {
+ @Override
+ public void onChange(boolean selfChange) {
+ mBatteryLevel.setVisibility((Settings.System.getInt(mContext.getContentResolver(),
+ "status_bar_show_battery_percent_outside", 1) != 0) ? View.VISIBLE : View.GONE);
+ }
+ };// ensure quick settings is disabled until the current user makes it through the setup wizardprivate boolean mUserSetup = false;
@@ -778,7 +789,8 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,// ================================================================================protected PhoneStatusBarView makeStatusBarView() {final Context context = mContext;
-
+ mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor("status_bar_show_battery_percent_outside"),
+ true, mBatteryLevelShow);updateDisplaySize(); // populates mDisplayMetricsupdateResources();@@ -914,6 +926,9 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,mLocationController = new LocationControllerImpl(mContext,mHandlerThread.getLooper()); // will post a notificationmBatteryController = createBatteryController();
+ mBatteryLevel = (TextView) mStatusBarView.findViewById(R.id.battery_level);
+ mBatteryLevel.setVisibility((Settings.System.getInt(context.getContentResolver(),
+ "status_bar_show_battery_percent_outside", 1) != 0) ? View.VISIBLE : View.GONE);mBatteryController.addStateChangedCallback(new BatteryStateChangeCallback() {@Overridepublic void onPowerSaveChanged(boolean isPowerSave) {
@@ -925,6 +940,9 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,@Overridepublic void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {// noop
+ String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0);
+ mBatteryLevel.setText(percentage);
+}});mNetworkController = new NetworkControllerImpl(mContext, mHandlerThread.getLooper());
@@ -3804,6 +3822,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,updateRowStates();mScreenPinningRequest.onConfigurationChanged();mNetworkController.onConfigurationChanged();
+ FontSizeUtils.updateFontSize(mBatteryLevel, R.dimen.battery_level_text_size);}@Override
这篇关于安卓7.0上在状态栏上加电池百分比的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!