Android-本地化及多语言设置&固定屏幕与旋转屏幕

本地化及多语言设置

在手机的设置中设置语言就可以看到效果了


固定屏幕与旋转屏幕

固定屏幕

加了此条配置,屏幕就不会旋转了

  • 总以竖屏显示:android:screenOrientation=“portrait”
  • 总以横屏显示:android:screenOrientation=“landscape”

旋转屏幕

正如前面在声明周期里说到的,当屏幕旋转的时候,会destory一次,重新开始构建,也就是说原本我们的屏幕显示的一些非默认数值在旋转后就会恢复默认值,这显然是不行的,此时就要借助onSaveInstanceState函数(下面还会使用ViewModel来进行管理数据);

Button button2;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    this.button2 = findViewById(R.id.button2);
    this.textView = findViewById(R.id.textView3);


    if(savedInstanceState!=null){
    
    
        //获取旋转前的数据进行显示
        String msg = savedInstanceState.getString("KEY");
        textView.setText(msg);
    }

    this.button2.setOnClickListener(v -> {
    
    
        this.textView.setText(R.string.button2);
    });

}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    
    
    super.onSaveInstanceState(outState);
    //保存旋转前的数据
    outState.putString("KEY",textView.getText().toString());
}

不管如何旋转,TextView中的字符串不会改变。

猜你喜欢

转载自blog.csdn.net/qq_44766883/article/details/112919825
今日推荐