你的位置:首页 > 操作系统

[操作系统]Android手机屏幕大小的获取


package com.example.testactivity;import android.app.Activity;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class TextActivity extends Activity {  private TextView tv;  private Button btn;  //获取手机屏幕分辨率的类  private DisplayMetrics dm;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    tv=(TextView)findViewById(R.id.textView1);    btn=(Button)findViewById(R.id.button1);    btn.setOnClickListener(new OnClickListener() {            @Override      public void onClick(View v) {        // TODO Auto-generated method stub        dm=new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(dm);      //获得手机宽高        String strPM="手机分辨率为:"+dm.widthPixels+"*"+dm.heightPixels;        tv.setText(strPM);      }    });  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;  }}

效果如图: