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

博客介绍了Android支持不同设备的方法。包括支持不同语言,需创建不同目录和字符串文件并使用字符串资源;支持不同屏幕,要创建不同布局和位图,系统会依屏幕密度选图;支持不同平台版本,可指定API级别、运行时检查系统版本及使用平台样式和主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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">

至此结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值