新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android開發(fā)中不得不了解知識點(diǎn)
對于這樣的布局,我們可以用GridView控件或RecyclerView控件來實(shí)現(xiàn),提倡用RecyclerView控件來實(shí)現(xiàn)了。一屏顯示16個應(yīng)用,這樣就需要和屏幕的知識點(diǎn)聯(lián)系上了。

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;
- }
這里對于底部,我們得花點(diǎn)心思,華為手機(jī)自帶下面有底部導(dǎo)航。我們要先通過判斷設(shè)備是否有返回鍵、菜單鍵(不是虛擬鍵,是手機(jī)屏幕外的按鍵)來確定是否有navigation bar
(已驗(yàn)證可行)。主要是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) {
- // 這個設(shè)備有一個導(dǎo)航欄
- return true;
- }
- return false;
- }
我們要獲取屏幕的總高度減去 頂部高度和底部導(dǎo)航的高度(設(shè)備存在底部導(dǎo)航),如果你的屏幕還有其他控件,也要一起減去,再平均四等分。這樣我們的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為頂部、導(dǎo)航和其他控件的總高度。這樣就能實(shí)現(xiàn)我們的需求了。
【本文為專欄作者“洪生鵬”的原創(chuàng)稿件,轉(zhuǎn)載請聯(lián)系原作者】
標(biāo)題名稱:Android開發(fā)中不得不了解知識點(diǎn)
網(wǎng)頁URL:http://m.fisionsoft.com.cn/article/djoidcd.html


咨詢
建站咨詢
