开发一个Android应用程序支持英语和阿拉伯语"布局调整"阿拉伯语、英语、应用程序、布局调整

2023-09-07 16:31:59 作者:丶糾結旳青春

我正在开发Android应用程序,同时支持恩/ AR。但我面临的一个问题,即如果从恩用户更改的Ar用户界面必须的调整,从左到右变为从右至左。

I am developing an Android app to support both En/Ar. But I faced a problem that if the user changes from En to Ar the alignment of the user interface must turns from "left to right" to "right to left".

例如:(适用的TextView)(EditText上)这是恩

但在氩它应该是:(EditText上)(TextView中)

有没有办法做到这一点的不会造成两种不同的布局或两个不同版本?

Is there a way to do this without creating two different layouts or two different versions?

推荐答案

为什么不能用这两种布局(你从来没有说为什么这不是渴望)做些什么呢?正如Android开发者文档中描述

Why can't this be done with two layouts (you never said why this is not desireable)? As described by the Android Developers documentation

本地化应用程序的很大一部分是提供替代  文本不同的语言。在某些情况下,你也将提供  另一种图形,声音,布局,以及其他特定于语言环境  资源。

A large part of localizing an application is providing alternative text for different languages. In some cases you will also provide alternative graphics, sounds, layouts, and other locale-specific resources.

应用程序可以,每个指定许多资源//目录  不同的预选赛。要创建一个替代资源  不同的区域设置,您可以使用限定符指定的语言或  语言区域的组合。

An application can specify many res// directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination.

参考:http://developer.android.com/guide/topics/resources/localization.html

所以你的情况创建 RES /布局-AR 那么你的现有布局复制到该文件夹​​,然后简单地交换他们圆。很简单,遵循最佳做法,是很容易做到。这也使得任何进一步的本地化更改更容易前进,而无需编写更多code。

So in your case create res/layout-ar then copy your existing layout into this folder and then simply swap them round. Simple, follows best practices and is easy to do. This also makes any further localisation changes easier going forward without having to write more code.

如果你写code你将需要找到设备的默认语言,然后交换在此基础上的意见。您可以通过获得的语言:

If you were to write code you are going to need to find the default language of the device and then swap the Views based on this. You can get the language by:

Locale.getDefault().getDisplayLanguage();

有关详细见这个问题:获取当前语言设备

See this question for more detail: Get the current language in device

在最后的个人注释:我认为前者是关注一个更好的分离,为code提供逻辑和XML布局实际控制布局(与Android自动地选择合适的资源,您无需写任何更多code)。

On a final personal note: I think the former is a much better separation of concerns, as the code provides logic and the XML layouts actually control the layout (with Android selecting the right resources automagically for you without having to write any more code).