android学习--开发文档(Training) 02

版权声明:转载标明来源 https://blog.csdn.net/qq_33347077/article/details/82056054

supporting different device

1.support different language

1.1 create different directories and string files

Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:

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

这个做法对于不同的语言有效,对于不同的其他资源文件也是可行的。

1.2 using the string resource

In your source code, you can refer to a string resource with the syntax R.string.<srting_name>

// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);

// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);

In other XML files, you can refer to a string resource with the syntax @string/<string_name>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

2. supporting different screen

Android categorizes device screens using two general properties: size and density.

There are four generalized sizes: small, normal, large, xlarge. And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)

Also be aware that the screens orientation (landscape or portrait) is considered a variation of screen size, so many apps should revise the layout to optimize the user experience in each orientation

android屏幕分为两种普遍的属性,分别是大小和分辨率(这里想想看电影就知道了)。并且为了实现app的表观最优化,我们需要在不同情况下设置不同的布局,而且手机的横屏和竖屏也需要考虑进去。

2.1 create different layout

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

和之前的加载不同语言的方法类似

2.2 create different bitmaps

一致,创建不同的内容物

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

Any time you reference @drawable/awesomeimage, the system selects the appropriate bitmap based on the screen’s density.

系统会根据手机屏幕的分辨率自动选择合适的图片进行加载

3. supporting different platform version

为了在老版本中使用一些最近的API,我们可以使用Android support Libaray

3.1 specify minimum and target API levels

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
    ...
</manifest>

在manifest.xml文件中配置了app的最低和最高兼容的API版本,为了适应最新出现的android style我们可以将最高API设置为最近的android版本

3.2 check system version at runtime

private void setUpActionBar() {
    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

在解析xml资源文件的时候,android会忽略掉不支持当前设备的一些属性。比如一些在高版本中的属性,在低版本中则不会解析直接忽略掉,所以不用担心低版本的崩溃问题。

3.3 use platform styles and themes

To make your activity look like a dialog box:

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

To make your activity have a transparent background:

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

To apply your own custom theme defined in /res/values/styles.xml:

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

To apply a theme to your entire app (all activities), add the android:theme attribute to the <application> element:

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

至此结束

猜你喜欢

转载自blog.csdn.net/qq_33347077/article/details/82056054
今日推荐