android locale, screen rotation

The content of the notes is the Android development tutorial for drinking (the latest version of 2019, using JetPack)

1. Set up a different locale

Insert picture description here
Added translations editor, and then we can click directly open editor, and then add the corresponding language in the graphical interface, first click the globe icon, then select the desired language, and finally add the corresponding meaning. Such as:
Insert picture description here

2. Screen rotation

Similarly, in the visual interface, you can set the corresponding layout file after the screen is rotated:
Insert picture description here
select,, and create landscape variationthen you can start writing the corresponding layout of the horizontal screen, such as:
Insert picture description here
Similarly, the corresponding layout file will be automatically loaded according to the system status .

2.1 Data saving of screen rotation

As the screen is rotated, the oncreatemethod will be re-executed to perform layoutthe rendering and loading of the layout file, so it is necessary to save some operations in the original vertical screen, and then load the original data in the horizontal screen.
It's very simple, only Bundlethe data in the storage is needed, and it can be rewritten specifically onSaveInstanceState, such as:

public class MainActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState!=null){
    
    
            String key = savedInstanceState.getString("Key");
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
    
    
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putString("KEY", "Value");
    }
}

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/113429119