Nút Align phải trong linearLayout ngang


130

Nếu bạn nhìn vào hình ảnh đính kèm. Tôi cần nút của mình được căn chỉnh đúng nhưng vì một số lý do, nó không hoạt động với 'lực hấp dẫn: đúng' ...

Hủy Thêm

Đây là mã của tôi cho bố cục đó:

<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>

Tại sao không hoạt động?!


6
Hãy thử bố trí tương đối trong trường hợp này. LL sẽ không hoạt động,
AAnkit

4
Nếu bạn muốn sử dụng linearLayout, hãy sử dụng hai linearlayouts bên trong và sử dụng layout_ weight cùng với layout_gravity. Nó sẽ hoạt động tốt.
Vamsi Challa

@VamsiChalla là có thể với một, kiểm tra câu trả lời mới của tôi dưới đây.
TWiStErRob

Câu trả lời:


137

Sử dụng mã dưới đây cho điều đó

<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>

3
Có vẻ như thuộc tính 'layout_alignParentRight' không có sẵn cho nút trong trường hợp này? Có thể không dùng nữa?

20
ru sử dụng relat khóayout thay vì linearlayout?
Dipak Keshariya

1
Ah crap .. Xin lỗi về điều đó, tôi hoàn toàn nhìn qua đó. Cảm ơn!

4
Tôi vẫn ngạc nhiên khi thấy một thứ dường như quá dễ dàng lại trở nên khó khăn đến vậy khi cố gắng sử dụng linearlayout.
AlvaroSantisteban

2
@DipakKeshariya cho khán giả trong tương lai, bạn có thể sử dụng "android: layout_gravity =" top | đúng" LinearLayout
SupimpaAllTheWay

145

LinearLayoutGiải pháp duy nhất. Chỉ thêm một chế độ xem khoảng cách đơn giản:
(Dường như không ai nhắc đến cái này, chỉ có các giải pháp đa bố cục phức tạp hơn.)

<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>

Lưu ý Chế độ xem "được tô sáng", tôi không sửa đổi bất cứ điều gì khác. Chiều cao = 0 đảm bảo nó không hiển thị. Width = 0 là do chiều rộng được xác định LinearLayoutdựa trên weight = 1. Điều này có nghĩa là chế độ xem spacer sẽ kéo dài hết mức có thể theo hướng (hướng) của LinearLayout.

Lưu ý rằng bạn nên sử dụng android.widget.Spacehoặc android.support.v4.widget.Spacethay vìView nếu mức API và / hoặc phụ thuộc của bạn cho phép. Spaceđạt được công việc theo cách rẻ hơn, bởi vì nó chỉ đo lường và không cố gắng vẽ bất cứ thứ gì như thế View; nó cũng biểu cảm hơn truyền đạt ý định.


2
Chỉ cần thêm android: layout_ weight = "1" vào R.id.lblExpense Hủy và đó là tất cả.
hrules6872

1
@h_rules hoạt động nếu bạn muốn hack, nhưng hãy tưởng tượng điều gì xảy ra khi bạn thêm nền vào TextView.
TWiStErRob

2
Tôi tự hỏi tại sao câu trả lời này lại bị downvote, nó hoạt động và nó rất hay, tôi chắc chắn sẽ thử nó.
Elvedin Hamzagic

Điều đó thật hoàn hảo khi là một nhà phát triển di động
Shivanand Darur

Nếu bạn thêm một miếng đệm, thì ở các kích cỡ màn hình khác nhau, bạn có kết quả khác nhau. Vì vậy, nó không phải là giải pháp lý tưởng.
GeoMint

29

Giải pháp thực sự cho trường hợp này:

android:layout_weight="1" cho TextView và nút của bạn di chuyển sang phải!


28

Tôi biết câu hỏi này đã được hỏi một lúc trước, nhưng tôi đã làm điều này trước đây và nó cho phép kiểm soát nhiều hơn khi sắp xếp các mục. Nếu bạn xem nó như lập trình web, nó sẽ giúp ích. Bạn chỉ cần lồng cái khác LinearLayoutsẽ chứa nút của bạn bên trong nó. Sau đó, bạn có thể thay đổi trọng lực của bố cục của nút và làm cho nó sang bên phải trong khi TextViewvẫn ở bên trái.

Hãy thử mã này:

<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>

Bạn có thể phải chơi với phần đệm trên bố cục lồng nhau để nó hoạt động theo cách bạn muốn.


2
Bạn nên sử dụng FrameLayout cho bố cục lồng nhau của bạn. Để định vị một yếu tố duy nhất là những gì nó làm. Và cũng, cho nó một trọng lượng để nó lấp đầy phần bên phải của cha mẹ. +1 vì không sử dụng Giao diện tương đối nào.
Hjalmar

10

thử cái này

 <?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>

9

Như đã đề cập một vài lần trước đây: Để chuyển từ công việc tuyến tính sang RelativeLayout, nhưng bạn cũng có thể giải quyết vấn đề thay vì tránh nó. Sử dụng các công cụ mà linearLayout cung cấp: Cung cấp cho TextView weight = 1 (xem mã bên dưới), trọng số cho nút của bạn sẽ vẫn là 0 hoặc không có gì. Trong trường hợp này, TextView sẽ lấy tất cả không gian còn lại, không được sử dụng để hiển thị nội dung của TextView hoặc ButtonView và nhấn nút của bạn sang bên phải.

<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>

2
Đây là câu trả lời tốt nhất. Tôi chỉ muốn viết một cái gì đó tương tự cho đến khi tôi tìm thấy bài viết của bạn.
mã hóa

Đó là cách tốt hơn so với chuyển sang RelativeLayout do vấn đề bộ nhớ, vì RelativeLayouttiêu thụ nhiều bộ nhớ hơnLinearLayout
ThunderWires

7

Bạn cần thêm trọng lực vào bố cục chứ không phải Nút, trọng lực trong cài đặt nút dành cho Văn bản bên trong nút

<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">

2
Điều đó sẽ không phù hợp với TextView đúng không? Tôi chỉ cần nút được căn chỉnh đúng ...

1
Nếu bạn cần căn chỉnh chỉ nút, sử dụng RelativeLayout.
biệt

1
Ngay cả khi bạn muốn tất cả các nút được căn chỉnh, điều này không hoạt động (API 16). Quan điểm của trẻ em vẫn được căn chỉnh bên trái.
m0skit0

5
<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>

Điều này sẽ giải quyết vấn đề của bạn


3

Nếu bạn không muốn, hoặc không thể, hãy sử dụng RelativeLayout, bạn có thể gói nút này trong linearLayout với hướng "dọc" và chiều rộng "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> 

Điều này là do nếu hướng của linearLayout là nằm ngang, trọng lực sẽ chỉ ảnh hưởng đến các khung nhìn theo chiều dọc. Và nếu hướng là 'dọc', trọng lực sẽ chỉ ảnh hưởng đến các chế độ xem theo chiều ngang. Xem ở đây để biết thêm chi tiết về giải thích định hướng / trọng lực của linearLayout.


2

Chỉ cần thêm android: vity = "right" bố cục dòng này sẽ hoạt động.

<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>

0

Tôi đã sử dụng một bố cục tương tự với 4 TextViews. Hai TextViews nên được căn chỉnh sang trái và hai TextViews nên được căn chỉnh sang phải. Vì vậy, đây là giải pháp của tôi, nếu bạn muốn sử dụng LinearLayoutsmột mình.

<?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>

0

Bạn có thể sử dụng RelativeLayout hoặc đặt lực hấp dẫn = "phải" trên bố trí chính của Nút.


0

Tôi biết cái này đã cũ nhưng đây là một cái khác trong Bố cục tuyến tính sẽ là:

<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" />

Xin lưu ý layout_gravity thay vì chỉ trọng lực.


Có chuyện center_vertical|bottom|topgì thế? Họ trông giống như trọng lực liên kết độc quyền với tôi.
TWiStErRob

0

Bạn nên sử dụng Relat khóayout thay vì Linearlayout làm bố cục chính. Nếu bạn sử dụng Relat khóayout là chính thì dễ dàng xử lý nút ở bên phải vì bố cục tương đối cung cấp cho chúng tôi alignParent phải, trái, trên và dưới.


Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.