我正在进行应用内语言更改,有时会将布局更改为RTL:
private void setLocale(Locale locale) {
Resources res = getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
conf.setLocale(locale);
conf.setLayoutDirection(locale);
} else {
conf.locale = locale;
}
res.updateConfiguration(conf, dm);
// relaunch the activity (there is some concerns using recreate(), so I'm using the classic way)
Intent intent = getIntent();
startActivity(intent);
finish();
}
设置新的Locale后,我重新启动整个活动,不仅重新创建,尽管如此,ToolBar和NavigationDrawer方向都没有改变 - 虽然活动视图本身发生了变化 - 在Android-Oreo中(API 26及更高版本)并且它们的方向仍然根据设备区域设置本身而不是应用程序区域设置,如果我更改了设备区域设置并重新打开应用程序,则它们将更改为新设备区域设置。 另一方面,一切都在Android-Oreo发布之前完成。
在xml中声明所有这些元素时,我已经将layoutDirection
属性设置为locale
,并且作为参考,以编程方式设置它们的代码如下:
/**
* To init and set the toolBar
*/
private void initToolbar() {
toolbar = findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT);
View v = getLayoutInflater().inflate(R.layout.ab_layout, null);
getSupportActionBar().setCustomView(v, layoutParams);
toolbar = (Toolbar) v.getParent();
toolbar.setContentInsetsAbsolute(0, 0);
}
/**
* For the navigation drawer
*/
private void initNavigationDrawer() {
// navigation View declaration
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
Drawer = findViewById(R.id.DrawerLayout); // Drawer object Assigned to the view
Drawer.setDrawerElevation(0);
mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
setNavigationDrawerCredentials(dbManager);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
}; // Drawer Toggle Object Made
Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
mDrawerToggle.syncState(); // Finally we set the drawer toggle sync State
}
答案 0 :(得分:0)
尝试为抽屉添加重力
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}