How to apply RTL dynamically and effectively

2019-07-27 06:12发布

问题:

I'm creating and adding TextViews in linear layout dynamically, whenever I'm applying layoutAmount.setRotationY(180); the layout changes its direction which is right, but the words of TextViews inside it also changes direction which is wrong, for example, if the word is sorry its becomes yrros, how can I apply RTL correctly

Thanks!

回答1:

setLayoutDirection will set layout direction according to the language

-Make sure that use layout gravity start and end

 public static void setLanguage(Context context, String language) {
            String[] localeLang = language.split("_");
            Locale locale;
            if (localeLang.length > 1)
                locale = new Locale(localeLang[0], localeLang[1]);
            else
                locale = new Locale(localeLang[0]);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                config.setLayoutDirection(locale);
                ((Activity) context).getWindow().getDecorView().setLayoutDirection(config.getLayoutDirection());
            }
            context.getResources().updateConfiguration(config,
                    context.getResources().getDisplayMetrics());
        }


回答2:

The best way to make layout dynamic RTL by following,

If you use ConstraintLayout

Don't use left and right

instead, use start and end

Android will change layout dynamically when you change locale

You can find my answer here about how to change the language of the app by locale

Update:

In ConstraintLayout Use Constrains as

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/imageView_about_back"
    app:layout_constraintTop_toTopOf="parent"

Also, don't use margin left or right

In case of LinearLayout,

use gravity start and end

sample code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_image"
    tools:context=".activity.HomeActivity">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:text="@string/about_al_ameen"
    android:textColor="@color/colorWhite" />
</LinearLayout>


回答3:

when you choose language add;

LocaleHelper.setLocale(LAngSelect.this, "ar");

Then save it with sqlite or other.

Then go to MainActivity with intent:

on MainActivity get the language saved and add:

if (langs.equals("ar")) {
                    forceRTLIfSupported();
                }

    private void forceRTLIfSupported()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){

            //HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then
            //change if it is english then don't

            getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);


        }
    }