以编程方式更改导航栏方向(RTL) [英] Change navigation bar orientation (RTL) programmatically

查看:166
本文介绍了以编程方式更改导航栏方向(RTL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序支持4种不同的语言,其中两种是从右到左书写的。



如果我从android系统更改语言然后启动我的应用程序,一切正常。我得到一个镜像布局(RTL)甚至导航栏都是镜像的。



我的问题是我的应用程序中有一个按钮来更改语言。
要以编程方式更改Locale,我这样做:

  Locale locale = new Locale(ar)
Locale.setDefault(locale);
配置config = new Configuration();
config.locale = locale;
getBaseContext()。getResources()。updateConfiguration(config,getBaseContext()。getResources()。getDisplayMetrics());

然后重新创建活动:

  this.recreate(); 

活动是从RTL重新创建的,但导航栏保留旧样式(未镜像):





是否可以通过编程方式镜像导航栏?



编辑(添加图片):



如果我更改系统语言,一切正常罚款:





如果我以编程方式更改语言,则导航栏未更改:





注意:



具体而言,开始在以下情况下表示正确:




  • 当前系统语言为RTL (阿拉伯语,希伯来语......)

  • 并且Android设备 API 17或更高



更多信息:


My application supports 4 different languages, being two of them written from right to left.

If I change the language from the android system and then start my application, everything works fine. I get a mirrored layout (RTL) and even the navigation bar is mirrored.

My problem is that I have a button in my application to change the language. To change the Locale programmatically I'm doing this:

Locale locale = new Locale("ar")
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

And then recreate the activity:

this.recreate();

The activity is recreated from RTL, but the navigation bar keeps the old style (not mirrored):

Is it possible to mirror the navigation bar programmatically?

EDIT (ADDING IMAGES):

If I change the system language everything work's fine:

If I change the language programmatically, the navigation bar isn't changed:

解决方案

From Android 4.2.2( API Level 17) it supports RTL natively.

When you are changing language pragmatically so how does the OS knows that you want RTL on specific language change. When the language is in RTL format. like when you change locale to Arabic then force system to do RTL and when the app closes then change back to original form.

To force your entire layout to be RTL including the navigation bar orientation (RTL) do the following. Edit your AndroidManifest.xml and add android:supportsRtl="true"

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
...
</application>

to your <application> tag and then add the following line to the top of your Activities' onCreate() method forceRTLIfSupported(); and then insert the follow function into your Activity.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
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);


    }
}

Output:

NOTE:

Specifically "start" means "right" if:

  • the current system language is RTL (Arabic, Hebrew...)
  • AND the android device is API 17 or higher

More info: Android Dev

这篇关于以编程方式更改导航栏方向(RTL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆