安卓虚拟按键(导航栏)适配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34161388/article/details/84259435

                                                                               

  网上关于适配虚拟按键的博客多如牛毛,我以前没做过相应的适配,最近新项目可以适配,然后从网上找了各种方案,试了很多,发现大部分都是渣渣,没什么卵用(哈哈,自己不会的大部分靠百度),但是最后还是找到了一个大神的博客,发现是有用的,在此万分感谢。但是与我的项目并不能完全适配,因为我项目是要求沉浸式状态,而我采用的方案跟这个方案有点冲突。

先大体讲下原理,本质就是根据屏幕的实际高度合理设置APP的显示高度,我们就从这个方向下手。首先上给跟布局设置id root_view,在onCreate中拿到root_view,然后传给工具类即可

AndroidWorkaround.assistActivity(getRootView(), this);

但是工具类要根据个人需求做更改。

public class AndroidWorkaround {
    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
    private Context context;

    /**
     * 关联要监听的视图
     *
     * @param viewObserving
     */
    public static void assistActivity(View viewObserving, Context context) {
        new AndroidWorkaround(viewObserving, context);
    }

    private View mViewObserved;//被监听的视图
    private int usableHeightPrevious;//视图变化前的可用高度
    private ViewGroup.LayoutParams frameLayoutParams;

    private AndroidWorkaround(View viewObserving, Context context) {
        mViewObserved = viewObserving;
        //给View添加全局的布局监听器
        mViewObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                resetLayoutByUsableHeight(computeUsableHeight());
            }
        });
        frameLayoutParams = mViewObserved.getLayoutParams();
        this.context = context;
    }

    private void resetLayoutByUsableHeight(int usableHeightNow) {
        //比较布局变化前后的View的可用高度
        if (usableHeightNow != usableHeightPrevious) {
            //如果两次高度不一致
            //将当前的View的可用高度设置成View的实际高度
            frameLayoutParams.height = usableHeightNow;
            mViewObserved.requestLayout();//请求重新布局
            usableHeightPrevious = usableHeightNow;
        }
    }

    /**
     * 计算视图可视高度
     *
     * @return
     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        mViewObserved.getWindowVisibleDisplayFrame(r);
        //适配沉浸式状态栏 增加状态栏的高度
        return (r.bottom - r.top + getStatusBarHeight());
    }

    private int getStatusBarHeight() {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}

在computeUsableHeight方法中,因为设置沉浸式状态栏导致屏幕实际高度变小了,所以加上了状态栏的高度,如果朋友们采用了我的沉浸式状态栏方案,就不需修改,如果没用沉浸式状态栏,把return加上的状态栏高度去掉即可。

沉浸式状态栏完美适配https://blog.csdn.net/qq_34161388/article/details/84258457,解决界面上移至状态栏或者刘海屏适配问题。

猜你喜欢

转载自blog.csdn.net/qq_34161388/article/details/84259435
今日推荐