2012-04-17 38 views
9

Làm cách nào để đặt hai nút trên cùng một dòng trong bố cục đăng nhập của tôi trên ứng dụng Android của tôi?Cách đặt hai nút trên cùng một dòng trong Android

+0

Bạn cần đặt khung câu hỏi của mình có ý nghĩa hơn. Hãy thử đưa ra một số chi tiết hơn về những gì bạn đã làm cho đến bây giờ. –

Trả lời

30

Chỉ cần tạo bố cục tuyến tính.Đặt hướng sang ngang và thêm hai nút.Đã sẵn sàng, bạn sẽ nhận được những gì bạn muốn. Trước khi đăng câu hỏi như vậy hãy thử googling bạn sẽ nhận được câu trả lời chắc chắn.Phần mã này sẽ giúp bạn .

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 

nếu bạn muốn vừa với hai nút trong bố cục, hãy tính trọng số là 1 cho cả hai nút.

+1

Đã làm việc cho tôi! – Axel

2

bạn có thể sử dụng một cách bố trí tuyến tính với định hướng horizonatal và thêm hai nút của bạn trong nó

<LinearLayout 

    <Button1.../> 
    <Button2.../> 
</LinearLayout> 
2

Bạn cần phải thêm tuyến tính Layout (ngang). sau đó bạn có thể thêm nhiều nút vào một dòng ....
Bạn cũng có thể sử dụng Bố cục tương đối cho điều này.
Đây là mã cho bạn ...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content"> 
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
</LinearLayout> 

1

Tôi nghĩ rằng bạn cần phải sử dụng RelativeLayout.You có thể làm một cái gì đó như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" android:orientation="horizontal" 
    android:layout_width="fill_parent"> 

    <Button 

     android:text="@+id/Button01" 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"> 
    </Button> 

    <Button 

     android:text="@+id/Button02" 
     android:id="@+id/Button02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"> 
    </Button> 

</RelativeLayout> 

Ngoài ra bạn có thể refert này là tốt . http://www.mkyong.com/android/android-relativelayout-example/

Hy vọng điều này sẽ giúp bạn.

1

Nếu bạn đang đặt các nút bên trong LinearLayout, hãy đặt giá trị Định hướng là "Dọc", nó sẽ tự động đặt các nút trong cùng một dòng. Nếu bạn đang sử dụng RelativeLayout, sau đó cho một nút sử dụng android: layout_toLeftOf HOẶC android: layout_toRightOf và cung cấp giá trị làm ID của nút khác. Nếu bạn đã làm đúng, hãy đánh dấu nó là câu trả lời. Cảm ơn ...

4

Sử dụng này đặt hai nút trên cùng một dòng ....

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    > 
    <Button 
    android:id="@+id/login" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Login" 
    android:layout_alignParentBottom="true" 
    /> 
     <Button 
    android:id="@+id/cancel"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Cancel" 
    android:layout_toRightOf="@+id/login" 
    android:layout_alignParentBottom="true" 
    /> 
     </RelativeLayout> 
5

Giải pháp tốt nhất là đặt 2 nút (Có chiều rộng bằng nhau) trong LinearLayout.

Một điều nữa, Nếu bạn muốn nút "chiều rộng" bằng nhau, hãy lấy nút có chiều rộng 0dp và cùng trọng số cho tất cả các nút.

Và nếu bạn muốn nút "hight" bằng nhau, hãy lấy nút có chiều cao 0dp và cùng trọng số cho tất cả các nút.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
+1

Tôi thực sự khuyên bạn nên giải pháp này. Sử dụng "layout_weight", là tùy chọn tốt nhất khi điều chỉnh các mục trên vùng chứa LinearLayout. – ivanleoncz

+1

Cảm ơn Ivan đã bình luận. –

Các vấn đề liên quan