您的位置:首页 > 移动开发 > Android开发

android偏门知识小节

2015-12-24 17:55 393 查看
1、dp转px,使用函数转换

Math.round(TypedValue.applyDimension(

                TypedValue.COMPLEX_UNIT_DIP,
10, getResources()

                        .getDisplayMetrics()));
2、系统导航栏:判读是否含有,获取高度
private boolean hasSystemNavigationBar() {

        if (Build.VERSION.SDK_INT < 11) {

            // 3.0加入的方法,3.0之前不处理

            return false;

        }

//        //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar

//        boolean hasMenuKey = ViewConfiguration.get(this)

//                .hasPermanentMenuKey();

//        boolean hasBackKey = KeyCharacterMap

//                .deviceHasKey(KeyEvent.KEYCODE_BACK);

//

//        if (!hasMenuKey && !hasBackKey) {

//            // 做任何你需要做的,这个设备有一个导航栏

//            return true;

//        }

        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

        boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);

        if (hasBackKey && hasHomeKey) {

            // 有虚拟按键

            return true;

        } else {

            // 没有有虚拟按键:99%可能。

            return false;

        }

//        return false;

    }

    public int getSystemNavigationBarHeight() {

        Resources resources = this.getResources();

        int resourceId = resources.getIdentifier("navigation_bar_height",

                "dimen", "android");

        //获取NavigationBar的高度

        int height = resources.getDimensionPixelSize(resourceId);

        return height;

    }

3、监听软键盘:onSizeChanged方法应该也可用

parentLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

            @Override

            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mNavigationLayout.getLayoutParams();

                if(bottom != 0 && oldBottom != 0 && bottom != oldBottom && layoutParams !=null){

                    if (height != 0 && bottom - oldBottom < 0 && oldBottom - bottom == height) {

                        // 如果有布局弹出,弹出高度,小于系统虚拟按键的高度,视为虚拟按键弹出,其他视为键盘

                        return;

                    }

                    if (bottom < oldBottom) {

4、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android