package com.example.examples_05_14;
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义DisplayMetrics对象
DisplayMetrics displayMetrics=new DisplayMetrics();
//取得窗口属性
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
//窗口宽度
int width=displayMetrics.widthPixels;
//窗口高度
int height=displayMetrics.heightPixels;
TextView textView=(TextView)this.findViewById(R.id.textView1);
textView.setText("屏幕的宽度:"+width+",高度"+height);
}
}
自动横屏和竖屏(代码写在AndroidManifest.xml)
android:name=".MainActivity"
android:label="@string/title_activity_main" android:screenOrientation="sensor">