Android 获得屏幕宽高的三种方式

所属分类: 软件编程 / Android 阅读数: 1930
收藏 0 赞 0 分享

老风格,废话不多说了,直接给大家贴android获取屏幕宽高的代码了。

主要代码:

package com.km.screeninfo;
  import android.os.Bundle;
  import android.support.v.app.Fragment;
  import android.support.v.app.FragmentActivity;
  import android.util.DisplayMetrics;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 public class MainActivity extends FragmentActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     if (savedInstanceState == null) {
       getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
     }
   }
   /**
    * A placeholder fragment containing a simple view.
    */
   public static class PlaceholderFragment extends Fragment {
     private TextView tvModel;
     private TextView tvScreenHeight;
     private TextView tvScreenHeight;
     private TextView tvScreenHeight;
     private TextView tvScreenWidth;
     private TextView tvScreenWidth;
     private TextView tvScreenWidth;
     private TextView tvScreenDensity;
     private TextView tvScreenDensityDPI;
     private TextView tvScreenDensity;
     private TextView tvScreenDensityDPI;
     private TextView tvScreenWidth;
     private TextView tvScreenHeight;
     public PlaceholderFragment() {
     }
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_main, container, false);
       tvModel = (TextView) rootView.findViewById(R.id.tv_model);
       tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height);
       tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height);
       tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height);
       tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height);
       tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width);
       tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width);
       tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width);
       tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width);
       tvScreenDensity = (TextView) rootView.findViewById(R.id.tv_screen_density);
       tvScreenDensityDPI = (TextView) rootView.findViewById(R.id.tv_screen_densityDPI);
       tvScreenDensity = (TextView) rootView.findViewById(R.id.tv_screen_density);
       tvScreenDensityDPI = (TextView) rootView.findViewById(R.id.tv_screen_densityDPI);
       tvModel.setText(android.os.Build.MODEL);
       // 获取屏幕密度(方法)
       int screenWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:px)
       int screenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:p)
       tvScreenHeight.setText(screenHeight + " px");
       tvScreenWidth.setText(screenWidth + " px");
       // 获取屏幕密度(方法)
       DisplayMetrics dm = getResources().getDisplayMetrics();
       float density = dm.density; // 屏幕密度(像素比例:./././.)
       int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:///)
       float xdpi = dm.xdpi;
       float ydpi = dm.ydpi;
       int screenWidth = dm.widthPixels; // 屏幕宽(像素,如:px)
       int screenHeight = dm.heightPixels; // 屏幕高(像素,如:px)
       tvScreenHeight.setText(screenHeight + " px");
       tvScreenWidth.setText(screenWidth + " px");
       tvScreenDensity.setText(density + "");
       tvScreenDensityDPI.setText(densityDPI + "");
       // 获取屏幕密度(方法)
       DisplayMetrics dm = new DisplayMetrics();
       getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
       density = dm.density; // 屏幕密度(像素比例:./././.)
       densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:///)
       xdpi = dm.xdpi;
       ydpi = dm.ydpi;
       tvScreenDensity.setText(density + "");
       tvScreenDensityDPI.setText(densityDPI + "");
       int screenWidth = dm.widthPixels; // 屏幕宽(px,如:px)
       int screenHeight = dm.heightPixels; // 屏幕高(px,如:px)
       tvScreenHeight.setText(screenHeight + " px");
       tvScreenWidth.setText(screenWidth + " px");
       float screenWidthDip = (dm.widthPixels / density); // 屏幕宽(dip,如:dip)
       float screenHeightDip = (dm.heightPixels / density); // 屏幕宽(dip,如:dip)
       tvScreenHeight.setText(screenHeightDip + " dip");
       tvScreenWidth.setText(screenWidthDip + " dip");
       return rootView;
     }
   }
 } 

 activity_main.xml:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.km.screeninfo.MainActivity"
   tools:ignore="MergeRootFrame" /> 
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.km.screeninfo.MainActivity$PlaceholderFragment" >
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
     android:orientation="vertical" >
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="手机型号:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_model"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="dip"
       android:layout_marginTop="dip"
       android:text="方法一:getDefaultDisplay().getH/W()"
       android:textColor="#aaaa"
       android:textSize="sp" />
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:layout_marginTop="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕高(一):"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_height"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕宽(一):"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_width"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="dip"
       android:layout_marginTop="dip"
       android:text="方法二:getResources().getDisplayMetrics().H/W"
       android:textColor="#aaaa"
       android:textSize="sp" />
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:layout_marginTop="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕高(二):"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_height"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕宽(二):"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_width"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:layout_marginTop="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="density:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_density"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="dip"
         android:text="densityDPI:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_densityDPI"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="dip"
       android:layout_marginTop="dip"
       android:text="方法三:getDefaultDisplay().getMetrics(DisplayMetrics).H/W"
       android:textColor="#aaaa"
       android:textSize="sp" />
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:layout_marginTop="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕高(三):"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_height"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕宽(三):"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_width"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:layout_marginTop="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="density:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_density"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="dip"
         android:text="densityDPI:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_densityDPI"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="dip"
       android:layout_marginTop="dip"
       android:text="屏幕宽高(px/density)"
       android:textColor="#aaaa"
       android:textSize="sp" />
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:layout_marginTop="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕高:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_height"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:orientation="horizontal" >
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="屏幕宽:"
         android:textStyle="bold" />
       <TextView
         android:id="@+id/tv_screen_width"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
     </LinearLayout>
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="dip"
       android:layout_marginTop="dip"
       android:text="科普:"
       android:textColor="#aaaa"
       android:textSize="sp" />
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:text="density = densityDpi/" />
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:text="px(pixels) = dip * (densityDpi/) = dip*density" />
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="dip"
       android:text="dip(device independent pixels) = dp " />
     <TextView
       android:layout_width="wrap_content"
       android:layout_margin="dip"
       android:layout_height="wrap_content"
       android:text="dip = (px * )/densityDpi = px / density" />
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="dip" />
   </LinearLayout>
 </ScrollView>

ps:在 Activity 中获得屏幕的宽和高

 int srceenW = this.getWindowManager().getDefaultDisplay().getWidth(); 
 int screenH = this.getWindowManager().getDefaultDisplay().getHeight(); 

如果需要适应不同分辨率的屏幕,则按照以下方式操作:

准备多套不同的图片:

分别放hdpi、mdpi、ldp文件夹中;

摆放控件的时候要得到屏幕宽高度,然后在程序中设定控件的百分乘屏幕的宽和高。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多