Operations on mobile phone screens in Android (obtaining screen width and height)

Source: Internet
Author: User

Here we will summarize the operations on the mobile phone screen in Android:

1. Get the screen resolution (because when processing the display in Android, you need to go to different la s or display different materials according to the screen resolution)

<Uses-SDK Android: minsdkversion = "8"/> there is a big difference between adding and not adding

// Obtain it through windowmanager
Displaymetrics dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
System. Out. println ("heigth:" + DM. heightpixels );
System. Out. println ("width:" + DM. widthpixels );
// Obtain from resources
Displaymetrics DM2 = getresources (). getdisplaymetrics ();
System. Out. println ("heigth1:" + dm2.heightpixels );
System. Out. println ("width1:" + dm2.widthpixels );

// Obtain the default screen resolution
Display display = getwindowmanager (). getdefaultdisplay ();
System. Out. println ("width-display:" + display. getwidth ());
System. Out. println ("heigth-display:" + display. getheight ());

2. Remove the screen title and full screen display

//
Remove title
Requestwindowfeature (window. feature_no_title );
// Set full screen
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen );

3. Set the screen direction
Configure activity attributes in the manifest. xml file

<Activity
Android: Name = ". animateactivity" Android: Label = "@ string/app_name"
Android: screenorientation = "Landscape"> <! -- Landscape portrait -->
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>


It is usually controlled in the oncreate and ondestroy methods in the activity, because when the screen direction changes,
The system restarts the activity. Therefore, you need to save the relevant data before the activity is destroyed to facilitate the next oncreate method.
Reload and update the screen Layout


Public
Void oncreate (bundle savedinstancestate ){
// Force horizontal screen
Setrequestedorientation (activityinfo. screen_orientation_landscape );
// Todo updates the screen Layout
}

Public void ondestroy (){
If (getrequestedorientation () =
Activityinfo. screen_orientation_landscape ){
// Save data
} Else if (getrequestedorientation () =
Activityinfo. screen_orientation_portrait ){
// Save data
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.