Android-20170410

来源:互联网 发布:淘宝网阿里旺旺打不开 编辑:程序博客网 时间:2024/06/05 00:50

Supporting Different Devices

1.支持多语言

  在res/values下,string.xml存储了相应字符串的name-value,且默认为英语。若要提供其他语言支持,如西班牙语,创建res/values-es,该文件夹下同样存在string.xml,保持name一致,改变value为对应的西班牙语即可。当改变手机的语言设置时,会对应匹配相应的语言。

  使用string.xml里的字符串资源:有如下xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="title">My Application</string>    <string name="hello_world">Hello World!</string></resources>
  使用格式:R.string.<string_name>

// Get a string resource from your app's ResourcesString hello = getResources().getString(R.string.hello_world);// Or supply a string resource to a method that requires a stringTextView textView = new TextView(this);textView.setText(R.string.hello_world);
  在其他xml文件中:

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

2.支持不同的屏幕

  与不同语言类似,在res文件夹中创建名称不同的资源文件夹,在这些文件夹中放入名称相同的资源文件,系统会根据设备自行选择。

3.支持不同的Android版本

  在AndroidManifest.xml文件里有描述支持的最小和最大ApiLevel

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />    ...</manifest>
在App运行时查看所用设备的ApiLevel:

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);    }}
用Build这个类。在查看Build源码时意外学到的一个java关键字native,简单描述:

就是用非java语言实现的method,只提供method申明,并不提供实现体,因为它在外部用其他语言实现。使用它可以方便的与外界交互,如底层硬件、操作系统等。

1 0