android4.4动态隐藏NavigationBar

最近试了下动态隐藏NavigationBar作下记录

在设置->显示里增加控制动态显示与隐藏Navigationbar的功能。

首先修改设置->显示增加相关图形界面

修改文件系统源码packages/apps/Settings/res/xml/display_settings.xml中增加

	<!--add by hclydao-->
        <CheckBoxPreference
            android:key="navbar"
            android:title="@string/hide_navbar_title"
            android:persistent="false" />
修改packages/apps/Settings/res/values/strings.xml增加

    <!--add by hclydao for hide navigationbar-->
    <string name="hide_navbar_title">Hide NavigationBar</string>
这样显示部分就出出来了,接着完善功能

修改packages/apps/Settings/src/com/android/settings/DisplaySettings.java增加如下内容

import android.content.Intent;

    private static final String KEY_NAVIGATION_BAR = "navbar";
    public static String ACTION_HIDE_NAV_BAR = "gzsd.hclydao.action.hide_nav_bar";
    public static String ACTION_SHOW_NAV_BAR = "gzsd.hclydao.action.show_nav_bar";

    private CheckBoxPreference mHideNav; //add by hclydao
加入初始化相关代码

		//add by hclydao
		mHideNav = (CheckBoxPreference) findPreference(KEY_NAVIGATION_BAR);
		mHideNav.setPersistent(false);
		//add end

		int navcheck = Settings.Global.getInt(resolver, Settings.Global.HIDE_NAVBAR,0);
		if(navcheck == 1)
			mHideNav.setChecked(true);

然后在onPreferenceTreeClick条件判断最后加上

        } else if(preference == mHideNav) { //add by hclydao
			boolean isCheck = mHideNav.isChecked();
			Intent mychange;
			if(isCheck) {
				Settings.Global.putInt(getContentResolver(), Settings.Global.HIDE_NAVBAR, 1);
		        mychange = new Intent(ACTION_HIDE_NAV_BAR);
			} else {
				Settings.Global.putInt(getContentResolver(), Settings.Global.HIDE_NAVBAR, 0);
		        mychange = new Intent(ACTION_SHOW_NAV_BAR);
			}
			getActivity().sendBroadcast(mychange);
		}
发送变化广播.

接着加入全局设置,让设置状态能够保存

修改frameworks/base/core/java/android/provider/Settings.java

在适合位置加入

public static final String HIDE_NAVBAR = "hide_navbar";
最后修改接收变化广播的地方:

frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

增加

	public static String ACTION_HIDE_NAV_BAR = "gzsd.hclydao.action.hide_nav_bar";
	public static String ACTION_SHOW_NAV_BAR = "gzsd.hclydao.action.show_nav_bar";
	//add by hclydao
    int[] oNavigationBarHeightForRotation = new int[4];
    int[] oNavigationBarWidthForRotation = new int[4];
这个用来保存原始的相关数据

在init最后增加

		//add by hclydao
        filter = new IntentFilter();
        filter.addAction(ACTION_HIDE_NAV_BAR);
        filter.addAction(ACTION_SHOW_NAV_BAR);
        context.registerReceiver(mHideNavBarReceiver, filter);
及广播接收类

//add by hclydao
    BroadcastReceiver mHideNavBarReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(ACTION_HIDE_NAV_BAR)) {
				hideNavBar();
				//Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.HIDE_NAVBAR, 1);
			} else if(intent.getAction().equals(ACTION_SHOW_NAV_BAR)) {
				showNavBar();
				//Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.HIDE_NAVBAR, 0);
			}
        }
    };
和相关操作函数

	//add by hclydao
	public void hideNavBar() {
		//Slog.d(TAG,"hideNavBar");
		if((mNavigationBarHeightForRotation[mPortraitRotation] == 0)
			&& (mNavigationBarHeightForRotation[mUpsideDownRotation] == 0)
			&& (mNavigationBarHeightForRotation[mLandscapeRotation] == 0)
			&& (mNavigationBarHeightForRotation[mSeascapeRotation] == 0))
			return;
		else {
			oNavigationBarHeightForRotation = mNavigationBarHeightForRotation;
			oNavigationBarWidthForRotation = mNavigationBarWidthForRotation;
			mNavigationBarHeightForRotation = new int[4];
			mNavigationBarWidthForRotation = new int[4];
		}
		updateRotation(false);
	}

	public void showNavBar() {
		//Slog.d(TAG,"showNavBar");
		mNavigationBarHeightForRotation = oNavigationBarHeightForRotation;
		mNavigationBarWidthForRotation = oNavigationBarWidthForRotation;
		updateRotation(false);
	}
最后在beginLayoutLw函数最后增加

		//hclydao
		int navcheck = Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.HIDE_NAVBAR,0);
		if(navcheck != 0)
			hideNavBar();

得到初始状态.

基本上修改完成,编译之前需要先更新一下api,效果如下:



============================================
作者:hclydao
http://blog.csdn.net/hclydao
版权没有,但是转载请保留此段声明

===========================================


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值