简体   繁体   English

工具栏和NavigationDrawer方向未随区域设置更改而改变-Oreo

[英]Toolbar & NavigationDrawer direction not changed with locale change - Oreo

I'm doing in-app language change which sometimes changes the layout to RTL: 我正在进行应用内语言更改,有时会将布局更改为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();
}

After setting the new Locale, I'm relaunching the whole activity not only recreating, despite that, both ToolBar and NavigationDrawer direction not changed - Although the activity view itself changed - in Android-Oreo(API 26 and later) and their direction remains according to the device locale itself not the app locale and if I changed the device locale and reopen the app, they are being changed to the new device locale. 设置新的区域设置后,我将重新启动整个活动,不仅重新创建,尽管如此,ToolBar和NavigationDrawer的方向均未更改-尽管活动视图本身已更改-在Android-Oreo(API 26及更高版本)中,它们的方向保持不变设备语言环境本身而不是应用程序语言环境,并且如果我更改了设备语言环境并重新打开应用程序,则它们将更改为新的设备语言环境。 On the other hand everything is going perfectly prior to Android-Oreo release. 另一方面,在Android-Oreo发行之前,一切进展顺利。

When declaring all of these elements in the xml, I already did set the layoutDirection attribute to locale , and for reference, the code for setting them programmatically is as following: 在xml中声明所有这些元素时,我已经将layoutDirection属性localelocale ,作为参考,以编程方式设置它们的代码如下:

/**
 * 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

}

try to add gravity for drawer 尝试增加抽屉的重力

if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    } else {
        mDrawerLayout.openDrawer(GravityCompat.START);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM