Android is adaptable to a different device

Source: Internet
Author: User

Thanks to the original author's collation:

http://blog.csdn.net/chenyjays/article/details/41308887

Adapting to a different language

  Store strings in the UI in an external file, extracted by code.

Creating locale directories and string files

In order to support multiple languages, create an additional values directory in res/named after the hyphen and ISO country code, such as values-es/is a directory where a simple resource file is placed for areas where the language code is "es". Android loads the appropriate resources at run time, depending on the locale of the device.

If you decide to support a language, you need to create a resource subdirectory and a string resource file, for example:

MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml

Adds a string value from a different region language to the appropriate file. At run time, the Android system uses the appropriate string resource based on the current locale of the user device.

Using Character resources
You can reference your string resource in your source code and other XML files through the Name property of the <string> element.
In your source code you can refer to a string resource through the r.string.<string_name> syntax, and many methods can accept the string in this way.

Adapt to a different screen

Android classifies the device screen as two general properties: size and resolution.

There are 4 universal sizes: Small (small), normal (normal), large (large), oversized (XLarge)
4 Universal resolutions: Low accuracy (LDPI), medium accuracy (MDPI), high accuracy (HDPI), ultra-high accuracy (XHDPI)

Create a different layout

To optimize the user experience for different screens, you need to create a unique XML file for each screen size that will be supported. Each layout needs to be saved in the appropriate resource directory, and the directory is named after the-<screen_size> suffix. For example, for a large screen (large screens), a unique layout file should be saved in res/layout-large/.

For example, this project contains a default layout and a layout that fits a large screen:

  MyProject/
      res/
          layout/
              main.xml
          layout-large/
              main.xml

The layout file name must be exactly the same, in order to provide the optimal UI for the corresponding screen size, the contents of the file are different.

Follow the conventions in your app to simply quote:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
  }

The layout is loaded in the corresponding layout directory based on the screen size of the device your app is running on.

Another example of this project is the layout for the adaptation of a landscape screen:

  MyProject/
      res/
          layout/
              main.xml
          layout-land/
              main.xml

By default, the layout/main.xml file is used as the layout of the vertical screen.

If you want to provide a special layout for the horizontal screen, and also for the large screen, then you need to use large and land modifiers.

   MyProject/
      res/
          layout/              # default (portrait)
              main.xml
          layout-land/         # landscape
              main.xml
          layout-large/        # large (portrait)
              main.xml
          layout-large-land/   # large landscape
              main.xml
Create a different bitmap

You should have 4 Universal resolutions: Low, Medium, high, and ultra-high precision, all of which provide the bitmap resources that are compatible with each other. To generate these images, you should start with the original vector image resources and then generate images of various densities based on the following size scales.

    • xhdpi:2.0
    • hdpi:1.5
    • mdpi:1.0 (Benchmark)
    • ldpi:0.75

This means that if you generate a 200x200 image for a xhdpi device, you should also generate 150x150 for hdpi, mdpi for 100x100, and ldpi for 75x75.

Then, put the files in the appropriate drawable resource directory:

  MyProject/
      res/
          drawable-xhdpi/
              awesomeimage.png
          drawable-hdpi/
              awesomeimage.png
          drawable-mdpi/
              awesomeimage.png
          drawable-ldpi/
              awesomeimage.png

At any time, when you reference, the @drawable/awesomeimage system chooses the appropriate bitmap based on the resolution of the screen.

Adapting to different system versions

Specifying the minimum and target API levels

The Androidmanifest.xml file describes the details of your app and identifies which Android versions the app supports. Specifically, the minsdkversion and Targetsdkversion attributes in the,<uses-sdk> element indicate the level of the least compatible API and the highest applicable API level when designing and testing the app (the highest level is the one that needs to pass your test )。

With the release of the new version of Android, some style and behavior may change, in order to enable your app to take advantage of these changes, and to adapt to different styles of user devices, you should set the value of Targetsdkversion to match the latest available Android version.

Check the system version at run time

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
          ActionBar actionBar = getActionBar();
          actionBar.setDisplayHomeAsUpEnabled(true);
  }

Use platform styles and themes

Android provides a user experience theme that provides the app with the look and feel of the underlying operating system. These themes can be applied to your app in the manifest file. By using built-in styles and themes, your app naturally automatically fits with the latest look and feel as the new version of Android is released.

Make your activity look like a dialog box:

  <activity android:theme="@android:style/Theme.Dialog">

Make your activity have a transparent background:

  <activity android:theme="@android:style/Theme.Translucent">

Apply the custom theme that is /res/values/styles.xml defined in:

  <activity android:theme="@style/CustomTheme">

Make the entire app apply a theme (all activities) to add attributes to the element android:theme :

  <application android:theme="@style/CustomTheme">


Android is adaptable to a different device

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.