android 获得屏幕宽度和高度

时间:2023-03-08 16:48:50
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <!-- 显示网络状态的标签控件 -->
<TextView
android:id="@+id/myyl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp" /> </RelativeLayout>
package com.example.yanlei.yl;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView pTextView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WindowManager wm = this.getWindowManager();
int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight();
pTextView=(TextView)findViewById(R.id.myyl);
pTextView.setText("屏幕宽度:"+width+",屏幕高度:"+height); } }