Android is very often used to obtain the screen size. For example, if you want to write a program and make a program with strong versatility, the screen is very applicable. Generally, the layout is defined based on the screen length and width, so I will summarize this to facilitate future reference when I forget it. In addition, sometimes writing a program requires no title. You can set a screen without title in the program! Reprinted please indicate the source:
Http://blog.csdn.net/wdaming1986/article/details/6769821
Program:
The Code illustrates all truths:
1. mainActivity. Java code:
Package com.cn. daming;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. Window;
Import android. view. WindowManager;
Import android. widget. TextView;
Www.2cto.com
Public class MainActivity extends Activity {
Private TextView mTextView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Set to untitled
RequestWindowFeature (Window. FEATURE_NO_TITLE );
// Set to full screen mode
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. main );
// Define the DisplayMetrics object
DisplayMetrics dm = new DisplayMetrics ();
// Obtain window properties
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
// Window width
Int screenWidth = dm. widthPixels;
// Window height
Int screenHeight = dm. heightPixels;
MTextView = (TextView) findViewById (R. id. text_view );
MTextView. setText ("screen width:" + screenWidth +
"\ N screen height:" + screenHeight );
}
}
Ii. main. xml layout file code:
<? 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/text_view"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_gravity = "center_vertical | center_horizontal"
Android: gravity = "center"
Android: text = "@ string/hello"
Android: textSize = "18pt"
/>
</LinearLayout>