android determine if device is in right to left language/layout

后端 未结 8 1895
予麋鹿
予麋鹿 2020-12-09 07:49

Is there a way to determine if the device is in a right to left language (something like Arabic) as opposed to something that\'s left to right (English)?

Something c

相关标签:
8条回答
  • 2020-12-09 08:29

    I think this is a better way:

    val isLeftToRight = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_LTR
    

    Docs:

    https://developer.android.com/reference/android/support/v4/text/TextUtilsCompat.html#getLayoutDirectionFromLocale(java.util.Locale)

    Or, if you have minAPI 17 or above, you can just use this:

    val isLeftToRight=TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())==View.LAYOUT_DIRECTION_LTR
    

    Docs:

    https://developer.android.com/reference/android/text/TextUtils.html#getLayoutDirectionFromLocale(java.util.Locale)

    0 讨论(0)
  • 2020-12-09 08:29

    If your Build.VERSION.SDK_INT is bigger than JELLY_BEAN (16) you can use this:

    val locale = Locale.getDefault() 
    val isLtr = TextUtils.getLayoutDirectionFromLocale(locale) == ViewCompat.LAYOUT_DIRECTION_LTR
    
    0 讨论(0)
  • 2020-12-09 08:31

    If this is still getting considerable traffic, people should also consider Configuration.getLayoutDirection(), for API 17+. It will return either LayoutDirection.LTR or LayoutDirection.RTL. I imagine ViewCompat.getLayoutDirection(android.view.View) returns the direction that was actually used to layout a particular view. If you're doing something weird, such as trying to determine which person should be on which side of a chat screen (like me), this might be of use.

    0 讨论(0)
  • 2020-12-09 08:34

    Gr, a little bit longer googling and I would have found it before posting:

    if (ViewCompat.getLayoutDirection(getView()) == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // ...
    }
    else {
        // ...
    }
    

    http://developer.android.com/reference/android/support/v4/view/ViewCompat.html#getLayoutDirection(android.view.View)

    0 讨论(0)
  • 2020-12-09 08:35

    Kotlin Code for detect app layout direction :

     val config = resources.configuration
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (config.layoutDirection == View.LAYOUT_DIRECTION_LTR) {
               // showAlertMsg("LTR")
            } else {
               // showAlertMsg( "RTL")
            }
        }
        else {
            //TODO("VERSION.SDK_INT < JELLY_BEAN_MR1")
               }
    
    0 讨论(0)
  • 2020-12-09 08:40

    You could create a values-ldrtl folder with a xml file called isrighttoleft.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="is_right_to_left">true</bool>
    </resources>
    

    and in your values folder the same file with:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="is_right_to_left">false</bool>
    </resources>
    

    And finally in Code:

    boolean isRightToLeft = getResources().getBoolean(R.bool.is_right_to_left);
    

    The values-ldrtl will only be used on a device where the specific settings (e. g. Language) are right-to-left-read languages.

    0 讨论(0)
提交回复
热议问题