簡體   English   中英

水平線性布局中的右對齊按鈕

[英]Right Align button in horizontal LinearLayout

如果您查看隨附的圖像。 我需要我的按鈕右對齊,但由於某種原因它不能與“重力:右”一起使用......

取消 添加

這是我的該布局的代碼:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

為什么沒有工作?!

單一LinearLayout解決方案。 只添加一個簡單的間距視圖:
(似乎沒有人提到這一點,只是更復雜的多布局解決方案。)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <!-------------------------  ADDED SPACER VIEW -------------------->
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <!------------------------- /ADDED SPACER VIEW -------------------->

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

注意“突出顯示”視圖,我沒有修改任何其他內容。 Height=0 確保它不可見。 Width=0是因為寬度是由LinearLayout根據weight=1決定的。 這意味着 spacer view 將在LinearLayout的方向(方向)上盡可能地拉伸。

請注意,如果您的 API 級別和/或依賴項允許,您應該使用android.widget.Spaceandroid.support.v4.widget.Space而不是View Space以一種更便宜的方式完成了這項工作,因為它只測量而不嘗試像View那樣繪制任何東西; 它也更能表達意圖。

使用下面的代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/stitch_button"
        android:text="@string/add" />

</RelativeLayout>

它的真正解決方案:

android:layout_weight="1" for TextView,你的按鈕向右移動!

我知道這個問題是不久前提出的,但我以前做過這個,它允許在對齊項目時進行更多控制。如果你像 Web 編程一樣看待它,它會有所幫助。 您只需嵌套另一個LinearLayout ,其中包含您的按鈕。 然后,您可以更改按鈕布局的重力並使其向右移動,而TextView仍在左側。

試試這個代碼:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"             
android:orientation="horizontal" 
android:layout_marginTop="35dp">

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel" 
    android:textColor="#404040"         
    android:layout_marginLeft="10dp" 
    android:textSize="20sp" 
    android:layout_marginTop="9dp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"                    
        android:text="@string/add" 
        android:layout_gravity="right" 
        android:layout_marginRight="15dp"/>

</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_gravity="right" 
    android:layout_height="wrap_content"             
    android:orientation="horizontal"  >

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"   
   android:orientation="horizontal" >

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="9dp"
    android:text="cancel"
    android:textColor="#ffff0000"
    android:textSize="20sp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="15dp"
     android:textColor="#ff0000ff"
    android:text="add" />

 </RelativeLayout>
  </LinearLayout>

正如之前多次提到的:從 LinearLayout 切換到 RelativeLayout 是可行的,但您也可以解決問題而不是避免它。 使用 LinearLayout 提供的工具:給 TextView 賦予 weight=1(參見下面的代碼),按鈕的權重應保持為 0 或無。 在這種情況下,TextView 將占用所有剩余空間,這些空間不用於顯示 TextView 或 ButtonView 的內容並將您的按鈕推到右側。

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="35dp">

        <TextView
            android:id="@+id/lblExpenseCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:textColor="#404040"
            android:layout_marginLeft="10dp"
            android:textSize="20sp"
            android:layout_marginTop="9dp"

            **android:layout_weight="1"**

            />

        <Button
            android:id="@+id/btnAddExpense"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:background="@drawable/stitch_button"
            android:layout_marginLeft="10dp"
            android:text="@string/add"
            android:layout_gravity="right"
            android:layout_marginRight="15dp" />

    </LinearLayout>

您需要將重力添加到布局而不是按鈕,按鈕設置中的重力適用於按鈕內的文本

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_gravity="right" 
android:layout_height="wrap_content"             
android:orientation="horizontal" 
android:layout_marginTop="35dp">
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"             
    android:orientation="horizontal" 
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="@string/cancel" 
        android:textColor="#404040"         
        android:layout_marginLeft="10dp" 
        android:textSize="20sp" 
        android:layout_marginTop="9dp"/>              

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"                    
        android:text="@string/add" 
        android:layout_gravity="right" 
        android:layout_marginRight="15dp"/>

</LinearLayout>

這將解決您的問題

只需添加android:gravity="right"這一行父布局就可以了。

<LinearLayout
        android:id="@+id/withdrawbuttonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        **android:gravity="right"**
        android:weightSum="5"
        android:visibility="visible">

        <Button
            android:id="@+id/bt_withDraw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_ADD"
            app:delay="1500" />

        <Button
            android:id="@+id/btn_orderdetail_amend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_CANCEL"
            app:delay="1500" />
</LinearLayout>

如果您不想或不能使用RelativeLayout,您可以將按鈕包裝在LinearLayout 中,方向為“vertical”,寬度為“fill_parent”。

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_marginTop="35dp">

  <TextView
      android:id="@+id/lblExpenseCancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/cancel"
      android:textColor="#404040"
      android:layout_marginLeft="10dp"
      android:textSize="20sp"
      android:layout_marginTop="9dp" />

  <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">
     <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />
  </LinearLayout>
</LinearLayout> 

這是因為如果 LinearLayout 的方向是水平的,重力只會垂直影響視圖。 如果方向是“垂直的”,重力只會影響水平方向的視圖。 有關 LinearLayout 方向/重力解釋的更多詳細信息,請參見此處

加入聚會很晚,但標准方法是使用 <Space>,它就是為此目的而創建的。

因此,在您的代碼中,只需在 TextView 和 Button 之間插入以下內容:

<Space
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1" 
/>

或者從調色板中,只需選擇布局 > 空間。 它會像上面的代碼一樣轉儲它,您只需要指定權重。

在此處輸入圖像描述

我使用了 4 個TextView的類似布局。 兩個TextView應該左對齊,兩個TextView應該右對齊。 所以,這是我的解決方案,如果你想單獨使用LinearLayouts

<?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:baselineAligned="false"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_weight="0.50"
        android:layout_height="match_parent"
        android:gravity="left"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_level"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/level"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_level_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_weight="0.50"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/time"
            android:textAppearance="?android:attr/textAppearanceMedium"
             />

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_time_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" 
            />
    </LinearLayout>

</LinearLayout>

您可以在 Button 的父布局上使用 RelativeLayout 或設置gravity="right"。

我知道這很舊,但線性布局中的另一個是:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp">

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel"
    android:textColor="#404040"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:layout_marginTop="9dp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:background="@drawable/stitch_button"
    android:layout_marginLeft="10dp"
    android:text="@string/add"
    android:layout_gravity="center_vertical|bottom|right|top"
    android:layout_marginRight="15dp" />

請注意 layout_gravity 而不是重力。

您應該使用 Relativelayout 而不是 Linearlayout 作為主布局。 如果您使用Relativelayout 作為main,那么可以輕松處理右側的按鈕,因為相對布局為我們提供了alignParent right、left、top 和bottom。

    Adding Spacing as View , makes the Last textview to right in 
    LinearLayout

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
     <!-- ADD SPACER VIEW -->
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    <!-- /ADD SPACER VIEW -->
</LinearLayout>

這將在不添加任何其他視圖的情況下工作。 您需要將您的線性布局高度設置為 wrap-content以便它占據所包含小部件s的最大尺寸。 然后我們將使用重力設置為 center_vertical對齊它。

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:gravity="center_vertical"
    android:paddingHorizontal="8dp"
    tools:context=".MainActivity">

    <Button
        android:paddingVertical="40"
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pause"
        />

    <Button
        android:id="@+id/resetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

在按鈕中使用布局寬度,如 android:layout_width="75dp"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM