如何在Android的布局中覆盖RTL支持

问题描述 投票:6回答:4

我在AndroidManifest.xml的android:supportsRtl="true"标记中设置了<application>,但是即使界面的语言是希伯来语或英语,我仍然必须强制其中一个视图为从左到右阿拉伯。如何在RTL应用程序中强制将特定视图设为LTR?

特别是,即使语言是从右到左,我也想强制某些线性布局从左到右,而不是默认的从右到左。

android android-layout right-to-left
4个回答
13
投票

通常gravity="left"足以迫使文本从左到右。但这对线性布局的方向没有帮助。解决方法是添加android:layoutDirection="ltr"


2
投票
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="ltr">

用于letf调整所有布局内容。


1
投票

为了完成答案,除了XML外,还可以使用ViewCompat.setLayoutDirection(view, LayoutDirection.RTL)以编程方式更改布局方向。此API可从API 19及更高版本开始使用,因此,如果您的最低SDK版本支持19以下的API,则需要执行if -check:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewCompat.setLayoutDirection(...)

1
投票

[如果即使在rtl语言上也要强制将应用程序转换为ltr,则不需要布局方向(它适用于api> 17,您可以将android:supportsRtl设置为false。

© www.soinside.com 2019 - 2024. All rights reserved.