Направление панели инструментов и NavigationDrawer не изменилось при изменении локали - Oreo - PullRequest
0 голосов
/ 13 мая 2018

Я делаю изменение языка в приложении, которое иногда меняет макет на 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();
}

После установки нового языкового стандарта я перезапускаю всю деятельность, а не только воссоздаю, несмотря на то, что обаНаправление 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

}

1 Ответ

0 голосов
/ 13 мая 2018

попробуйте добавить гравитацию для ящика

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