[英]how to create android linearlayout with RTL horizontal orientation
我想創建一個水平方向的線性布局
但我希望它能讓孩子們像往常一樣對齊,而不是左側
我怎樣才能做到這一點?
我知道如何使用relativeLayout執行此操作,但我想練習linearLayout
您可以使用4.2(jelly bean)中引入的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
如果您使用Android studio,則可以執行以下操作:
1-右鍵單擊項目主文件夾
2-重構
3-添加對RTL的支持
您可以使用此代碼段來反轉布局視圖。
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));
}
或者要開始在應用程序中支持RTL布局,請將android:supportsRtl屬性設置為清單文件中的元素並將其設置為“true”。啟用此功能后,系統將啟用各種RTL API以使用RTL布局顯示您的應用程序。例如,操作欄將在右側顯示圖標和標題,在左側顯示操作按鈕,您使用框架提供的View類創建的任何布局也將顛倒。請查看此Android doc
您可以將父視圖元素的重力設置為右側
<?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.