![](/img/trans.png)
[英]How to align view in layout using linear layout in horizontal orientation in android
[英]How to right align widget in horizontal linear layout Android?
這是我正在使用的代碼,但它不起作用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
</LinearLayout>
嘗試在要正確View
元素之前在水平LinearLayout
添加空View
,例如:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如果您不希望所有內容都正確,請不要將 LinearLayout 的重力更改為“正確”。
嘗試:
fill_parent
right
代碼:
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
作為 alcsan 答案的補充,您可以使用自 API 14 (Android 4.0 ICE_CREAM_SANDWICH) 起的Space
,請在此處記錄。
Space 是一個輕量級的 View 子類,可用於在通用布局中創建組件之間的間隙。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="right" />
</LinearLayout>
對於支持低於 14 的 API 級別的應用程序,自 Android 支持庫 r22.1.0 以來,有一個android.support.v4.widget.Space
。
使用線性布局
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_car_book_tabbar"
android:gravity="right" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/my_booking_icon" />
</LinearLayout>
使用框架布局
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_car_book_tabbar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:src="@drawable/my_booking_icon" />
</FrameLayout>
使用相對布局
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_car_book_tabbar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:src="@drawable/my_booking_icon" />
</RelativeLayout>
設置視圖的layout_weight="1"
就可以了。!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
將android:gravity="right"
到 LinearLayout。 假設TextView
有layout_width="wrap_content"
只需在您的班輪布局中添加android:gravity="right"
。
我已經通過最簡單的方式做到了:
只需取一個RelativeLayout並將您的子視圖放入其中,您想將其放置在右側。
<LinearLayout
android:id="@+id/llMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f5f4f4"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="20dp">
<ImageView
android:id="@+id/ivOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/txtOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Hiren"
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="@android:color/black" />
<RelativeLayout
android:id="@+id/rlRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:id="@+id/ivRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
希望它會幫助你。
具有layout_width="fill_parent"
linear layout
以及與gravity as right
具有相同layout width
+ gravity as right
的小部件會將其與右側對齊。
我使用的是2 TextView
在下面的例子中S, topicTitle
左側和topicQuestions
在右邊。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/topicTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/topicQuestions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
對於具有水平方向的LinearLayout
,將layout_weight
賦予其他子視圖,但要右對齊的視圖除外。 這完美地工作。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Specialization"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:textColor="#ff0000" />
</LinearLayout>
您應該使用 RelativeLayout 並拖動它們直到看起來不錯:)
<ImageView
android:id="@+id/button_info"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:contentDescription="@string/pizza"
android:src="@drawable/header_info_button" />
</RelativeLayout>
嘗試將 layout_width 更改為android:layout_width="match_parent"
因為gravity:"right"
對齊 layout_width 內的文本,如果您選擇 wrap content 它沒有去哪里,但如果您選擇 match parent 它可以去正確的。
無需使用任何額外的視圖或元素:
//就是這么簡單
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
//這是左對齊
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No. of Travellers"
android:textColor="#000000"
android:layout_weight="1"
android:textStyle="bold"
android:textAlignment="textStart"
android:gravity="start" />
//這是右對齊
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:layout_weight="1"
android:textAlignment="textEnd"
android:gravity="end" />
</LinearLayout>
如果使用 TextView:
<TextView
android:text="TextView"
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity">
</TextView>
添加視圖有點困難,它覆蓋了所有屏幕寬度,如下所示:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
試試這個代碼:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Account"/>
</LinearLayout>
要在LinearLayout
開始對齊一個元素,在LinearLayout
結尾對齊一個元素,您可以將其包裝在RelativeLayout
。
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:weightSum="2">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textColor="@android:color/background_dark"
android:backgroundTint="@android:color/transparent"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_dark"
android:backgroundTint="@android:color/transparent"
android:text="Save"/>
</RelativeLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
此示例的結果如下:鏈接到圖像
注意:您可以將任何您想要的東西包裹在里面並對齊。
這是我的 xml,要右對齊的動態組件,在我的情況下,我使用 3 個按鈕
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkinInputCodeMember">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="vertical" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/bttn_extends"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="@color/colorAccent"
android:text="3"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/bttn_checkout"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="@color/colorAccent"
android:text="2"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/checkinButtonScanQrCodeMember"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="@color/colorAccent"
android:text="1"/>
</LinearLayout>
結果
您可以隱藏右側的第一個按鈕並更改可見性 GONE,這是我的代碼
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkinInputCodeMember">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="vertical" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/bttn_extends"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="@color/colorAccent"
android:text="3"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/bttn_checkout"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="@color/colorAccent"
android:text="2"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/checkinButtonScanQrCodeMember"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="1"
android:textColor="@color/colorAccent"
**android:visibility="gone"**/>
</LinearLayout>
仍然右對齊,在可見性消失后第一個右組件
這是一個示例。 關鍵安排如下
android:layout_width="0dp"
android:layout_weight="1"
完整代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/categoryName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="abcd" />
<TextView
android:id="@+id/spareName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="efgh" />
</LinearLayout>
使用 match_parent 和引力將 TextView 文本設置為右側,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
</LinearLayout>
試試這個..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="right" >
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.