Android : Change entire app layout directions prog

2020-02-21 02:09发布

How can I change entire app layout direction to RTL? I am writing an app that user must select it's language in first launch and the layout should change based on user selection to RTL or remains LTR. I used to add android:supportsRtl="true" to the AndroidManifest and android:layoutDirection="rtl" for each layout but this approach have some problems like below :

One problem is when I change the direction to RTL the ActionBar home icon or navigation button (when home as up is enabled) remains LRT and just move to the right.

I also tried to change direction programmatically and the result was the same :

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindows().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

enter image description here

Is there any way to force all activities to become RTL at once or we should set direction in each activity separately?

7条回答
Lonely孤独者°
2楼-- · 2020-02-21 02:26

Following code will work:

    Resources your_resource = context.getResources();

    Configuration config = your_resource.getConfiguration();
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLayoutDirection(locale);
    }

where locale is object of Locale

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-21 02:30

Try code below if you want to change language in the app and change directions in views and resources on active layout:

// set Persian language in the app
setLanguage(context, "fa");

public void setLanguage(Context c, String lang) {
    Locale localeNew = new Locale(lang);
    Locale.setDefault(localeNew);

    Resources res = c.getResources();
    Configuration newConfig = new Configuration(res.getConfiguration());
    newConfig.locale = localeNew;
    newConfig.setLayoutDirection(localeNew);
    res.updateConfiguration(newConfig, res.getDisplayMetrics());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        newConfig.setLocale(localeNew);
        c.createConfigurationContext(newConfig);
    }
}

Also, after changing the language, you need to change the directions for the already created views on the active layout. You can do this with this code:

if (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL) {
        view.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
} else {
        view.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
查看更多
闹够了就滚
4楼-- · 2020-02-21 02:35

You can use this piece of code while your application's minSdk >= 17.

I used fa for Farsi, you can use other rtl language.

Configuration configuration = getResources().getConfiguration();
configuration.setLayoutDirection(new Locale("fa"));
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
查看更多
你好瞎i
5楼-- · 2020-02-21 02:35

add this class:

public class LanguageHelper {

public static void changeLocale(Resources res, String locale) {

    Configuration config;
    config = new Configuration(res.getConfiguration());

    switch (locale) {
        case "fa":
            config.locale = new Locale("fa");
            config.setLayoutDirection(new Locale("fa"));
            break;
        case "en":
            config.locale = Locale.ENGLISH;
            config.setLayoutDirection(new Locale("en"));
            break;
    }
    res.updateConfiguration(config, res.getDisplayMetrics());
}

public static Locale getLocale(Resources res) {
    Configuration config;
    config = new Configuration(res.getConfiguration());
    return config.locale;
}
}

then use above class like this(in your activity):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //========set default language as persian:start==============
    LanguageHelper.changeLocale(this.getResources(), "fa");
    //========set default language as persian:end================

  ........
 }

and your layout will be RTL.

查看更多
The star\"
6楼-- · 2020-02-21 02:39

I also have this problem, i find that if your view's width set MATCH_PARENT, you should set the view's gravity with Gravity.LEFT in LTR model and set the view's gravity with Gravity.RIGHT in RTL model.

查看更多
beautiful°
7楼-- · 2020-02-21 02:42

Change the whole layout

 LinearLayout linrtl=(LinearLayout)findViewById(R.id.linrtl);
 linrtl.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
查看更多
登录 后发表回答