简体   繁体   English

如何使用RTL水平方向创建android linearlayout

[英]how to create android linearlayout with RTL horizontal orientation

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 我知道如何使用relativeLayout执行此操作,但我想练习linearLayout

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的支持

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM