底部导航栏:
1.隐藏底部导航栏
——————————————————————————————————————————
2.导航栏动态隐藏、显示
1、frameworks/base/core/java/android/content/Intent.java
文件,在文件末尾插入如下代码:
2、
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java中添加过滤广播
3、在广播mBroadcastReceiver中添加判断项
private void setSystemBarVisibility(int visibility) {
if (DEBUG) Log.v(TAG, "setSystemBarVisibility: " + visibility);
if (visibility == View.GONE && mNavigationBarView != null) {
try {
mWindowManager.removeViewImmediate(mNavigationBarView);
Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY , 0);
Log.d("daibin","statusbar setSystemBarVisibility gone");
} catch (IllegalArgumentException e) {
Log.w(TAG, "IllegalArgumentException: " + e);
}
mNavigationBarView = null;
} else if (visibility == View.VISIBLE && mNavigationBarView == null) {
try {
createNavigationBar();
Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY , 1);
Log.d("daibin","statusbar setSystemBarVisibility visibility");
} catch (WindowManager.BadTokenException e) {
// ignore
Log.w(TAG, "BadTokenException: " + e.getMessage());
} catch (RuntimeException e) {
// don't crash if something else bad happens, for example a
// failure loading resources because we are loading from an app
// on external storage that has been unmounted.
Log.w(TAG, "RuntimeException: " + e);
}
}
}
4、系统设置添加导航栏隐藏、显示功能
(1)找到packages/apps/Settings/res/values-zh-rCN/strings.xml
文件,插入以下代码:
<!-- System Bar settings title-->
<string name="system_bar_settings_title">系统栏显示</string>
<!-- System Bar settings summary-->
<string name="system_bar_settings_summary">控制系统栏显示或隐藏</string>
(2)找到apps/Settings/res/values/strings.xml
文件,插入以下代码:
<!-- System Bar settings title-->
<string name="system_bar_settings_title">System Bar Display</string>
<!-- System Bar settings summary-->
<string name="system_bar_settings_summary">Control System Bar show or hide</string>
(3)找到packages/apps/Settings/res/xml/display_settings.xml
文件,插入以下代码:
<SwitchPreference
android:key="navigation_bar"
android:title="@string/system_bar_settings_title"
android:summary="@string/system_bar_settings_summary"
android:persistent="false" />
注意:SwitchPreference放置的位置决定了显示的位置
5、找到frameworks/base/core/java/android/provider/Settings.java,添加以下内容
6、新建导航栏隐藏、显示控制类
位置:packages/apps/Settings/src/com/android/settings/display/
public class NavigationBarDisplayController extends AbstractPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String KEY_NAVIGATION_DISPLAY = "navigation_bar";
private Preference mPreference;
private Context mContext;
public NavigationBarDisplayController(Context context) {
super(context);
mContext = context;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SYSTEM_BAR_SHOW);
filter.addAction(Intent.ACTION_SYSTEM_BAR_HIDE);
mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
}
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_SYSTEM_BAR_SHOW.equals(action)) {
if (mPreference != null)
((SwitchPreference) mPreference).setChecked(true);
} else if (Intent.ACTION_SYSTEM_BAR_HIDE.equals(action)) {
if (mPreference != null)
((SwitchPreference) mPreference).setChecked(false);
}
}
};
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_NAVIGATION_DISPLAY;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mPreference == null) mPreference = preference;
boolean value = (Boolean) newValue;
mContext.sendBroadcast(new Intent(value ? Intent.ACTION_SYSTEM_BAR_SHOW : Intent.ACTION_SYSTEM_BAR_HIDE));
Log.d("daibin", "onPreferenceChange putInt");
//Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY, value ? 1 : 0);
return true;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (mPreference == null) mPreference = preference;
// update navigation bar
int value = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY, 1);
((SwitchPreference) preference).setChecked(value == 1);
}
}
注意:getPreferenceKey()要和布局中的SwitchPreference的key对应起来
7、找到
packages/apps/Settings/src/com/android/settings/DisplaySettings.java
添加以下内容
8、用法:
// 显示
adb shell am broadcast -a android.intent.action.SYSTEM_BAR_SHOW
// 隐藏
adb shell am broadcast -a android.intent.action.SYSTEM_BAR_HIDE
或者
// 显示
Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_BAR_SHOW");
sendBroadcast(intent);
// 隐藏
Intent intent = new Intent();
intent.setAction("android.intent.action.SYSTEM_BAR_HIDE");
sendBroadcast(intent);