<Android 基础(十八)> XLIFF

介绍

XLIFF ,XML Localization Interchange File Format,XML本地化数据交换格式。


实际使用

1.布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="mraz.com.uiwidgetdemo.MainActivity">


        <TextView
            android:id="@+id/tv_hello"
            android:textSize="10pt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    </LinearLayout>
</ScrollView>

2.String资源文件

<resources xmlns:xliff="http://schemas.android.com/tools">
    <string name="hello">your name is <xliff:g id="NAME">%1$s</xliff:g>, and your age is<xliff:g id="AGE">%2$s</xliff:g></string>

    <string name="hello_simple">your name is %1$s, and your age is %2$s</string>
</resources>

两种,一种是直接使用%1$s类似的格式,另外一种就是使用 xliff:g 给需要替换的内容加了个名字而已,效果是相同的。
属性id可以随便命名
%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格
%n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0
%n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00


3.程序中使用

TextView textView= (TextView) findViewById(R.id.tv_hello);

String hello =  getString(R.string.hello_simple, "android", "28");

textView.setText(hello);

结果:

这里写图片描述

原文地址:https://www.cnblogs.com/lanzhi/p/6467184.html