日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android获取屏幕尺寸,屏幕适配

發布時間:2023/12/10 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android获取屏幕尺寸,屏幕适配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

獲取屏幕尺寸:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w = dm.widthPixels;//寬
int h = dm.heightPixels;//高
Log.i("lgq","ww==="+w+"....h==="+h);

輸出:2019-07-22 11:47:30.283 27564-27564/com.tianxin.okhttptest I/lgq: ww===1080....h===1792

屏幕適配實現效果

平板大屏效果? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 手機屏幕效果

對比:

實現步驟

非常簡單。加入資源文件,layout.xml引用即可,包括尺寸適配,字體適配

demo資源鏈接:Android屏幕適配資源_Android如何獲取屏幕自適應大小-Android代碼類資源-CSDN下載

module連接:android開發尺寸適配-Android文檔類資源-CSDN下載?

?

工具類

/*** @ClassName DensityUtil* @Description 用于手機適配的一些類* @author XiongChuanLiang<br/>(xcl_168@aliyun.com)*/public class DensityUtil {//private static final String TAG = "DensityUtil";private DensityUtil() {}public static float getDensity(Context context) { return context.getResources().getDisplayMetrics().density;}/** * 根據手機的分辨率從 dp 的單位 轉成為 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根據手機的分辨率從 px(像素) 的單位 轉成為 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** 屏幕寬度*/public static int getScreenWidth(Context context){return context.getResources().getDisplayMetrics().widthPixels;}/*** 屏幕高度*/public static int getScreenHeight(Context context){return context.getResources().getDisplayMetrics().heightPixels;}/* * 獲取控件寬 */ public static int getWidth(View view) { int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(w, h); return (view.getMeasuredWidth()); } /* * 獲取控件高 */ public static int getHeight(View view) { int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(w, h); return (view.getMeasuredHeight()); } }

總結

以上是生活随笔為你收集整理的Android获取屏幕尺寸,屏幕适配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。