Java 如何创建具有 RTL 水平方向的 android 线性布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20632852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create android linearlayout with RTL horizontal orientation
提问by Elad Benda
I want to create a linear layout with horizontal orientation
我想创建一个水平方向的线性布局
but I want it to position its children aligned to its right and not to its left as usual
但我希望它像往常一样将它的孩子定位在它的右边而不是它的左边
how can I do this?
我怎样才能做到这一点?
I know how to do this using relativeLayout, but I want to practice linearLayout
我知道如何使用相对布局来做到这一点,但我想练习线性布局
采纳答案by harun
You can use android:layoutDirection attribute which is introduced in 4.2(jelly bean).
您可以使用 4.2(jelly bean) 中引入的 android:layoutDirection 属性。
The following links will help.
以下链接将有所帮助。
http://developer.android.com/reference/android/view/View.html#attr_android:layoutDirection
http://developer.android.com/reference/android/view/View.html#attr_android:layoutDirection
http://android-developers.blogspot.co.uk/2013/03/native-rtl-support-in-android-42.html
http://android-developers.blogspot.co.uk/2013/03/native-rtl-support-in-android-42.html
回答by Adhikari Bishwash
you can use this code snippet to reverse your layout views.
您可以使用此代码片段来反转您的布局视图。
LinearLayout ll = // inflate
ArrayList<View> views = new ArrayList<View>();
for(int x = 0; x < ll.getChildCount(); x++) {
views.add(ll.getChildAt(x));
}
ll.removeAllViews();
for(int x = views.size() - 1; x >= 0; x--) {
ll.addView(views.get(x));
}
OR To begin supporting RTL layouts in your app, set the android:supportsRtl attribute to the element in your manifest file and set it “true". Once you enable this, the system will enable various RTL APIs to display your app with RTL layouts. For instance, the action bar will show the icon and title on the right side and action buttons on the left, and any layouts you've created with the framework-provided View classes will also be reversed. Look at this android doc
或 要开始在您的应用中支持 RTL 布局,请将 android:supportsRtl 属性设置为清单文件中的元素并将其设置为“true”。启用此功能后,系统将启用各种 RTL API 以显示具有 RTL 布局的应用。例如,操作栏将在右侧显示图标和标题,在左侧显示操作按钮,并且您使用框架提供的 View 类创建的任何布局也将被反转。 看这个 android doc
回答by Rick Royd Aban
You can set on the parent view element's gravity to right
您可以将父视图元素的重力设置为右侧
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" <!-- make sure this is not wrap_content !-->
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
</LinearLayout
回答by Fareed Alnamrouti
if you are using Android studio you can do this:
如果您使用的是 Android Studio,您可以这样做:
1- right click on the project main folder
1-右键单击项目主文件夹
2- refactor
2-重构
3- add support for RTL
3- 添加对 RTL 的支持