简体   繁体   中英

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

You can use android:layoutDirection attribute which is introduced in 4.2(jelly bean).

The following links will help.

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

if you are using Android studio you can do this:

1- right click on the project main folder

2- refactor

3- add support for 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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