DisplayMetrics获取手机屏幕大小(宽高)

res/layout/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="@string/hello"
    />
</LinearLayout>

src/EX03_05.java

package gphone.ex03_05;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class EX03_05 extends Activity {
    /** Called when the activity is first created. */
	TextView tv=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DisplayMetrics dm=new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        String str="手机屏幕的分辨率为"+dm.widthPixels+" x "+dm.heightPixels;
        tv=(TextView)this.findViewById(R.id.tv);
        tv.setText(str);
    }
}

显示结果

image

原文地址:https://www.cnblogs.com/AlexCheng/p/2120052.html