欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中獲取手機(jī)屏幕大小的方法

 更新時(shí)間:2015年12月22日 16:06:06   作者:cjjky  
這篇文章主要介紹了Android中獲取手機(jī)屏幕大小的方法,Android開發(fā)需要獲得屏幕的寬高,本文為大家解析 Android中如何獲取手機(jī)屏幕大小,需要的朋友可以參考下

本文為大家解析Android中如何獲取手機(jī)屏幕大小,提供一個(gè)解決方法,分享給大家供大家參考,具體內(nèi)容如下

運(yùn)行效果圖:

運(yùn)行程序后,當(dāng)我們點(diǎn)擊Button按鈕時(shí),可以看到下面的效果圖:

具體代碼:

我們可以通過使用類DisplayMetrics來獲取手機(jī)屏幕的分辨率大小。DisplayMetrics類是獲取手機(jī)屏幕各種屬性的關(guān)鍵類。下面通過例子來展示如何獲取手機(jī)屏幕的分辨率。
在布局文件main.xml中添加一個(gè)TextView對(duì)象,一個(gè)Button對(duì)象。其中TextView對(duì)象用來顯示獲得的分辨率值,Button對(duì)象是當(dāng)點(diǎn)擊時(shí)獲取分辨率。main.xml的代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
  <TextView  
   android:id="@+id/tv"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"  
   android:text="手機(jī)分辨率為:"/> 
  <Button 
    android:id="@+id/btnOK" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="獲取手機(jī)分辨率"/> 
</LinearLayout> 

在TestActivity中的代碼如下:

public class TestActivity extends Activity { 
     
  private TextView tv; 
  private Button btn; 
   
  //獲取手機(jī)屏幕分辨率的類 
   private DisplayMetrics dm; 
   
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
     
    tv = (TextView)findViewById(R.id.tv); 
    btn = (Button)findViewById(R.id.btnOK); 
    btn.setOnClickListener(new View.OnClickListener() { 
       
     public void onClick(View v) { 
       dm = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 
         //獲得手機(jī)的寬度和高度像素單位為px 
     String strPM = "手機(jī)屏幕分辨率為:" + dm.widthPixels+"* "+dm.heightPixels; 
     tv.setText(strPM); 
      } 
    }); 
  } 
  
} 

希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。

相關(guān)文章

最新評(píng)論