[Android] 008_得到当前手机屏幕的宽度和高度

Android_008_得到当前手机屏幕的宽度和高度

用下列代码可以获得手机屏幕的分辨率(宽度和高度), 代码如下:

package com.shy;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

publicclass ScreenActivity extends Activity {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得窗口的信息
WindowManager manager = getWindowManager();
Display display
= manager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
//利用控件ID 获得控件对象
TextView width = (TextView)findViewById(R.id.width);
TextView height
= (TextView)findViewById(R.id.height);
//将信息用TextView显示出来
width.setText("宽度:"+ screenWidth);
height.setText(
"高度:"+ screenHeight);
}
}

2. 布局文件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/width"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
<TextView
android:id="@+id/height"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
</LinearLayout>

运行后显示的结果如下:

clip_image001

posted @ 2011-07-12 23:49  ShanHaiyang  阅读(376)  评论(0编辑  收藏  举报