对于这样的发中布局,我们可以用GridView控件或RecyclerView控件来实现,不得不解提倡用RecyclerView控件来实现了。知识一屏显示16个应用,发中这样就需要和屏幕的不得不解知识点联系上了。
1、知识获取顶部status bar 高度
/** * 获取顶部status bar 高度 */ private int getStatusBarHeight() { Resources resources = mActivity.getResources(); int resourceId = resources.getIdentifier("status_bar_height",发中 "dimen","android"); int height = resources.getDimensionPixelSize(resourceId); Log.v("dbw", "Status height:" + height); return height; }2、获取底部 navigation bar 高度
private int getNavigationBarHeight() { Resources resources = mActivity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height",不得不解"dimen", "android"); int height = resources.getDimensionPixelSize(resourceId); Log.v("dbw", "Navi height:" + height); return height; }这里对于底部,我们得花点心思,知识华为手机自带下面有底部导航。发中我们要先通过判断设备是不得不解否有返回键、高防服务器菜单键(不是知识虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
(已验证可行)。主要是发中KeyEvent.KEYCODE_BACK和hasPermanentMenuKey
@SuppressLint("NewApi") public static boolean checkDeviceHasNavigationBar(Context activity) { boolean hasMenuKey = ViewConfiguration.get(activity) .hasPermanentMenuKey(); boolean hasBackKey = KeyCharacterMap .deviceHasKey(KeyEvent.KEYCODE_BACK); if (!hasMenuKey && !hasBackKey) { // 这个设备有一个导航栏 return true; } return false; }我们要获取屏幕的总高度减去 顶部高度和底部导航的高度(设备存在底部导航),如果你的不得不解屏幕还有其他控件,也要一起减去,知识再平均四等分。源码下载这样我们的RecyclerView的每个itemView才能均等。
3、屏幕高度
DisplayMetrics displayMetrics = new DisplayMetrics(); Activity activity = (Activity) mContext; activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); LayoutParams layoutParams = holder.itemView.getLayoutParams(); layoutParams.height = (displayMetrics.heightPixels - topHeight)/4; holder.itemView.setLayoutParams(layoutParams);其中topHeight为顶部、导航和其他控件的总高度。这样就能实现我们的需求了。
【本文为专栏作者“洪生鹏”的原创稿件,转载请联系原作者】
戳这里,看该作者更多好文
服务器托管