Ultra-detailed! Step by step to complete the multi-language adaptation

Multi-language adaptation work is often use to, for fear of their own forgotten, and to look at multi-language adaptation we write this blog, I did not talk much ------------ -let's start.

Multi-language adaptation in three steps: 1, the new values ​​file 2, Java Code 3, refresh the page


First, create a new document in different languages ​​Values

The first step, the working range on the left to switch to the project, find the values ​​res file folder under the folder copy and paste the following frame to res file rename
As shown below:


When you click OK in the corresponding multi-res folder named so after a while explanation as to why ~
As shown below:


colors.xml and styles.xml not use deleted

Open two new strings.xml a string convenient for a confirmation


In English it to the      values-en



The first step basically completed, but take the opportunity to make a brief document layout set it

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.q.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/asd"
        android:textSize="40sp"
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:text="刷新按钮"
        android:textSize="40sp"/>


</LinearLayout>




Two, Java code,


Resources resources = getResources();
        //获取系统的配置
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        //将语言设置成简体中文
        config.locale = Locale.CHINESE;
        resources.updateConfiguration(config, dm);


Penultimate line Locale. Behind with what language is the language what if you built a Chinese values-zh-rCN This will become the system default
Replaced Locale.CHINESE you will become the beginning of a system comes strings.xml  

Run after completing a change and found no words I own set? ? ?
Why ------ Because there is no page refresh - -.

Third, refresh the page 

The above layout file which has a Button remember? ?
Set in the Java code inside the click event
Which code is as follows


btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                overridePendingTransition(0,0);
            }
        });


Jump Animation hard to read, I could cancel become a bit of flash ------ 

Well now is not the language to run on the exchange of it ha ha 

So now the question came ~~~~~~ different languages, different values ​​corresponding folder named how should it?
This is a good question.
For more details, look at a blog
88










发布了124 篇原创文章 · 获赞 141 · 访问量 16万+

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/74517538