国际化
Application
public class MyApplacation extends Application {
@Override
public void onCreate() {
super.onCreate();
//TODO 1:新的语言
String language = getSharedPreferences("language", MODE_PRIVATE).getString("language", "bo");
Locale locale = new Locale(language,"");
//TODO 2:配置信息
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
//TODO 3:设置新的语言
configuration.locale=locale;
//TODO 4:更新
resources.updateConfiguration(configuration,resources.getDisplayMetrics());
}
}
代码
public class MainActivity extends AppCompatActivity {
Button bt_chinese,bt_english;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_chinese=findViewById(R.id.bt_chinese);
bt_english=findViewById(R.id.bt_english);
bt_chinese.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSharedPreferences("a",MODE_PRIVATE)
.edit()
.putString("a","bo")
.commit();
}
});
bt_english.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSharedPreferences("a",MODE_PRIVATE)
.edit()
.putString("a","en")
.commit();
}
});
}
}
xml
屏幕适配
❗注意:所有控件的dp,sp都要是px,清单文件的宽高要和UI一样,activity要继承autoActivity
依赖
implementation 'com.zhy:autolayout:1.4.5'
清单文件
<meta-data android:name="design_width" android:value="768"></meta-data>
<meta-data android:name="design_height" android:value="1280"></meta-data>