Android:以编程方式更改整个应用布局的方向 [英] Android : Change entire app layout directions programmatically
问题描述
如何将整个应用程序布局方向更改为RTL?
我正在编写一个应用程序,用户必须在首次启动时选择其语言,布局应根据用户选择更改为RTL或保持LTR.
我曾经为每种布局将android:supportsRtl="true"
添加到AndroidManifest
和android:layoutDirection="rtl"
中,但是这种方法存在如下问题:
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 :
一个问题是,当我将方向更改为RTL时,ActionBar
主页图标或导航按钮(启用主页向上功能时)仍保持LRT并向右移动.
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);
}
是否有任何方法可以强制所有活动立即变为RTL,或者我们应该分别为每个活动设定方向?
Is there any way to force all activities to become RTL at once or we should set direction in each activity separately?
推荐答案
您可以在应用程序的 minSdk> = 17 时使用这段代码.
You can use this piece of code while your application's minSdk >= 17.
我将fa
用于Farsi
,您可以使用其他 rtl 语言.
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());
这篇关于Android:以编程方式更改整个应用布局的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!