Android screen adaptive orientation and android adaptive Orientation

Source: Internet
Author: User

Android screen adaptive orientation and android adaptive Orientation

Recently, I feel like I am getting stuck on the screen ......
The Android phone is Xiami. How many resolutions can it have? Why !!!!!!! Amen ......
There are two solutions ......
First:
HTML5 + CSS3 + WebView interaction ...... I am still studying it. I am leaving the demo empty.
Second:
Use the android UI's own layout to handle the following:
1. Screen Layout
Create different layout (drawable is also supported) folders under the res directory, such as layout-640x360, layout-800x480, all layout files will be written to R after compilation. java, and the system will select the appropriate layout based on the screen size.
2. Multi-Resolution Support
Supports mdpi, ldpi, and hdpi files with multiple resolutions.
Hdpi stores high-resolution images, such as WVGA (480x800) and FWVGA (480x854, tangle !!!!)
The mdpi contains medium-resolution images, such as HVGA (320x480 );
Ldpi stores low-resolution images, such as QVGA (240x320 ).
The system will go to these folders to find the corresponding images based on the machine resolution. In program development, in order to be compatible with different screens of different platforms, we recommend that you store images of different versions in the corresponding folders as needed.
3. Get the screen size

Java code  
  1. DisplayMetrics metric = new DisplayMetrics ();
  2. GetWindowManager (). getDefaultDisplay (). getMetrics (metric );
  3. Int width = metric. widthPixels; // screen width (pixels)
  4. Int height = metric. heightPixels; // screen height (pixels)
  5. Float density = metric. density; // screen density (0.75/1.0/1.5)
  6. Int densityDpi = metric. densityDpi; // screen density DPI (120/160/240)


In other words, on a low-density cell phone, the correct size cannot be obtained only by relying on the above Code. Therefore, add the supports-screens node to the AndroidManifest. xml file of the project. The specific content is as follows:

Xml Code  
  1. <Supports-screens
  2. Android: smallScreens = "true"
  3. Android: normalScreens = "true"
  4. Android: largeScreens = "true"
  5. Android: resizeable = "true"
  6. Android: anyDensity = "true"/>


4. There are also automatic horizontal screens and so on, not supported ...... En ~ Amen ...... Android: screenOrientation = "portrait" All vertical...


How does the Android program adapt to the orientation and size of the screen?

Different Android targets have different sizes. the application interface needs to adjust the size of interface elements for different sizes. In addition, the screen can be switched between the landscape screen and the portrait screen, and the interface also needs to be adjusted. By default, when the screen is switched, The onCreate () method of the activity will be called again, so you can use the following code to read the screen direction: public void onCreate () {if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {Log. I ("info", "landscape");} else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT) {Log. I ("info", "portrait") ;}} if you are in androidmanifest. add android: configChanges = "orientation | keyboardHidden | navigation in xml. When the screen is flipped, the Activity will not repeatedly call onCreate (), onPause (), and onResume (). instead, call onConfigurationChanged (Configuration newConfig) int screenWidth, screenHeight; WindowManager windowManager = getWindowManager (); Display display = windowManager. getdefadisplay display (); screenWidth = display. getWidth (); screenHeight = display. getHeight (); someone also mentioned another method: DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm );

In android, how does one make the program adaptive to the screen size?

This is a problem involving screen resolution. First, you have to get the screen size of the device and then call resources that are closer to the device. This resource is the folders in the res directory of your project, than the film has three folders, placed in different sizes, the following layout XML, as shown in: layout-240x480, the xml corresponds to the corresponding size of the image; do not know if you have any help

Related Article

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.