ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android基础——国际化

2021-09-17 19:59:46  阅读:199  来源: 互联网

标签:xml 控件 国际化 layout 语言 基础 Android strings


国际化

国际化是什么?

国际化指的是当Android系统切换语言时,应用程序也随之改变,以适应不同的国家地区

控件国际化

修改activity_main.xml,创建三个按钮,从左到右为123

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

    <Button
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

    <Button
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv1"
        android:text="2" />

    <Button
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv2"
        android:text="3" />
</RelativeLayout>

效果如下图
在这里插入图片描述
然而有些国家的文字是从右往左读的,当调整系统语言后,app内的控件也应该随之变化,在AS中可通过如下图方式预览,但在运行时须在manifest文件下的<application>标签添加如下属性

android:supportsRtl="true"

按道理应该从右往左为123,但2和3哪去了?因为2和3设置了android:layout_toRightOf="@id/tv1",所以调整后2和3仍在其右边,但溢出屏幕导致看不到
在这里插入图片描述
为保证从右往左情况下的正确布局,需要将layout_toRightOf换成layout_toEndOf(同理所有left属性都要换成start属性),如下为正常情况
在这里插入图片描述

语言国际化

当改变系统语言时,app的语言也要随着改变,在res-values-strings.xml创建字符串"str_hello"

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">demo0</string>
    <string name="str_hello">hello</string>
</resources>

在activity对其引用

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/str_hello" />
</RelativeLayout>

strings.xml下默认存放英语,如果要存放别的语言,我们应该另外创建strings.xml(右键values-New-Values Resource file)
在这里插入图片描述
随后选择语种(可按键搜索)和地区
在这里插入图片描述
这时会生成同名带后缀的strings.xml,我们将不带后缀的默认strings.xml里的属性替换成别的语言
在这里插入图片描述
这样当系统切换语言时就会自动引用并切换到对应的语言

在这里插入图片描述
在这里插入图片描述

标签:xml,控件,国际化,layout,语言,基础,Android,strings
来源: https://blog.csdn.net/qq_35258036/article/details/120353118

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有