2011-11-12 76 views
13

Tôi chỉ tìm thấy tôi không thể xóa khoảng trống giữa 2 nút ngay cả khi tôi đặt layout_marginRightlayout_marginLeft như dưới đây. Nhưng nó có ý nghĩa nếu tôi đặt không gian lớn hơn như 10 dp. Bất kỳ cách nào để giải quyết nó?Android: cách làm cho nó không có khoảng trống giữa 2 nút trong một linearlayout ngang

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" android:padding="0dp" 
    android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp"> 
    <Button android:id="@+id/LocationTitleButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_weight="1" 
        android:ellipsize="end" 
        android:gravity="center_vertical" 
        android:scrollHorizontally="true" 
        android:singleLine="true" 
        android:text="Add location" 
        android:textStyle="bold" /> 
       <Button android:textColor="#FF000000" 
        android:layout_weight="0" 
        android:id="@+id/AddLocationButton" 
        android:text="Search" 
        android:gravity="center_vertical" 
        android:layout_gravity="center_vertical" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="0dp" /> 
</LinearLayout> 
+1

Bạn nên đặt layout_width của "0dip" cho mỗi nút. Tôi hy vọng nó sẽ giúp. –

+0

nó không hoạt động, tôi sẽ làm cho nút bên phải biến mất vì trọng lượng của nó là 0. –

+0

Có cần phải đặt trọng số của nút thứ hai thành 0 không? –

Trả lời

38

Vui lòng xem Bryan's answer. Với câu trả lời của tôi cả hai nút chồng lên nhau. Câu trả lời của Bryans cho thấy kích thước thật của các nút.

Cũ câu trả lời:

Chỉ cần đặt android:layout_marginRight của nút đầu tiên để "-8dip" hoặc thậm chí nhiều hơn nữa. Hơn không gian giữa hai nút sẽ nhỏ hơn.

+0

nó thực sự phức tạp nhưng nó hoạt động, thx! –

+5

lợi nhuận tiêu cực cần tránh .. – Muzammil

+2

@Muzammil: Bạn có thể đưa ra lời giải thích cho ý kiến ​​của bạn không? –

1

Bạn sẽ phải đặt android:layout_marginRight="0dip" và bạn sẽ phải xóa phần đệm bằng android:paddingRight="0dip" cho nút khác này phải được thay đổi thành giá trị bên trái. Tôi đoán bạn đã quên rằng mỗi phần tử android thường có một phần đệm được thêm vào nó theo mặc định. Đây thường là một ý tưởng hay, nhưng nếu bạn muốn loại bỏ nó, đây là cách.

+0

Tôi đã thử, nó không hoạt động để loại bỏ khoảng cách giữa 2 nút. Phần đệm dành cho khoảng trống bên trong nút. –

2

Bạn có thể chuyển sang RelativeLayout. Không có khoảng trống trong Bố cục đó.

0

Tôi nghĩ bạn có thể loại bỏ không gian nếu bạn sử dụng TableLayout thay thế. Và bạn có thể đặt giá trị âm cho lề, nếu nó vẫn thêm một số khoảng trống mặc định giữa chúng.

23

Thử thay đổi màu của nút, vì giao diện mặc định của nút gốc Android thực sự nhỏ hơn kích thước của nó và được đặt ở giữa để làm cho nó trông đẹp mắt.

Thay đổi nền thành màu đen hoặc gì đó và bạn sẽ thấy kích thước thực của nút.

android:background="#000" 
+3

Điều này phải được chấp nhận câu trả lời. Câu trả lời của Franziskus thực sự không phải là một thực hành tốt. – Drag0

0

Sử dụng "layout_marginLeft" & "layout_marginRigh" để điền vào nút nền

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:gravity="bottom" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/imageButton1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="-5dp" 
     android:layout_marginLeft="-3dp" 
     android:layout_marginRight="-4dp" 
     android:layout_weight="1" 
     android:drawableTop="@drawable/create_mail" /> 

    <Button 
     android:id="@+id/bItem" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="-5dp" 
     android:layout_marginLeft="-4dp" 
     android:layout_marginRight="-4dp" 
     android:layout_weight="1" 
     android:drawableTop="@drawable/email_receive3" 
     android:onClick="OnClick" 
     android:text="@string/inbox" /> 


    <Button 
     android:id="@+id/imageButton2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="-5dp" 
     android:layout_marginLeft="-4dp" 
     android:layout_marginRight="-3dp" 
     android:layout_weight="1" 
     android:drawableTop="@drawable/email_trash" /> 


</LinearLayout> 
Các vấn đề liên quan