zl程序教程

您现在的位置是:首页 >  APP

当前栏目

Android入门之支持不同的设备

2023-03-14 10:26:49 时间

支持不同的设备包括:

一、支持不同的语言

应用程序中的UI部分字符串定义在res/values/strings.xml文件中,这是一个很好的习惯!

MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml
不同的语言要放在合适的目录下,例如:

English (default locale), /values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
</resources>

Spanish, /values-es/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mi Aplicación</string>
    <string name="hello_world">Hola Mundo!</string>
</resources>

French, /values-fr/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Mon Application</string>
    <string name="hello_world">Bonjour le monde !</string>
</resources>

二、支持不同的屏幕尺寸及分辨率

  • 一般有4种屏幕尺寸: small, normal, large, xlarge
  • 一般有4种屏幕分辨率: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)
  • MyProject/
        res/
            layout/
                main.xml
            layout-land/
                main.xml
    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
    • xhdpi: 2.0
    • hdpi: 1.5
    • mdpi: 1.0 (baseline)
    • ldpi: 0.75
    如果你为xhdpi分辨率的设备生成一个200*200的图片,那么相同的图片在hdpi设备中的分辨率为150*150,在mdpi中是100*100,在ldpi中是75*75.
  • 接下来,把图片放在相对应的目录下
  • MyProject/
        res/
            drawable-xhdpi/
                awesomeimage.png
            drawable-hdpi/
                awesomeimage.png
            drawable-mdpi/
                awesomeimage.png
            drawable-ldpi/
                awesomeimage.png

三、支持不同的系统平台版本

指定最小和目标API级别:

 AndroidManifest.xml 中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>

    <uses-sdkandroid:minSdkVersion="4"android:targetSdkVersion="15"/>

    ...

</manifest>

2、在运行时检查系统版本:

private void setUpActionBar(){

    // Make sure we're running on Honeycomb or higher to useActionBar APIs

    if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB){

        ActionBar actionBar= getActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);

    }

}

3、使用平台风格和主题:

使用对话框主题:

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

使用透明背景的主题:

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

 /res/values/styles.xml 下定义自己的风格主题:

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

如果要定义所有页面的风格主题,则把 android:theme 添加到<application> 元素中:

<applicationandroid:theme="@style/CustomTheme">

更多风格和主题可以看http://developer.android.com/guide/topics/ui/themes.html