2012-02-26 39 views
13

Tôi cần tạo thanh công cụ của riêng mình trong ứng dụng Android. Có vẻ như sau: enter image description hereXóa khoảng cách giữa các nút trong LinearLayout (Android)

Vì vậy, bạn thấy khoảng cách giữa các nút. Tôi đã cố gắng đặt lề/đệm âm ở các nút, nhưng không gian không biến mất.

Đây là mã bố trí:

<?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="vertical" > 

    <ListView 
     android:id="@+id/routes_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5"> 
    </ListView> 

    <TableRow 
     android:id="@+id/toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:padding="0dp" > 

     <Button 
      android:id="@+id/btn_routes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:text="@string/routes" 
      android:textSize="10dp" /> 

     <Button 
      android:id="@+id/btn_places" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:textSize="10dp" 
      android:text="@string/places" /> 

     <Button 
      android:id="@+id/btn_events" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:textSize="10dp" 
      android:text="@string/events" /> 

     <Button 
      android:id="@+id/btn_about" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:text="@string/about" 
      android:textSize="10dp" /> 

    </TableRow> 

</LinearLayout> 

Làm thế nào để loại bỏ khoảng cách giữa các nút?

+0

Có lẽ bạn có thể kiểm tra điều này - http://stackoverflow.com/questions/2277732/how-to-get-rid-of-the-extra-gap-between-a-button-and-other-views –

+0

Ý tưởng là sử dụng ImageButton với cùng màu nền. – hovanessyan

Trả lời

8

Dưới đây là một giải pháp. Có vẻ như tôi đã vội vã đăng câu hỏi ở đâ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_height="fill_parent" 
     android:orientation="vertical" > 

    <ListView 
     android:id="@+id/routes_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5"> 
    </ListView> 

    <TableRow 
     android:id="@+id/toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom= "-7dp"> 

     <Button 
      android:id="@+id/btn_routes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4.5dp" 
      android:layout_marginLeft="-5dp" 
      android:text="@string/routes" 
      android:textSize="10dp" /> 

     <Button 
      android:id="@+id/btn_places" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4.5dp" 
      android:layout_marginLeft="-4.5dp" 
      android:textSize="10dp" 
      android:text="@string/places" /> 

     <Button 
      android:id="@+id/btn_events" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4.5dp" 
      android:layout_marginLeft="-4.5dp" 
      android:textSize="10dp" 
      android:text="@string/events" /> 

     <Button 
      android:id="@+id/btn_about" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4dp" 
      android:layout_marginLeft="-4.5dp" 
      android:text="@string/about" 
      android:textSize="10dp" /> 

    </TableRow> 

</LinearLayout> 

Kết quả: enter image description here

+6

Tôi không nghĩ rằng điều này sẽ làm việc trên mọi thiết bị. –

+0

Câu trả lời không tốt, với kiểu nút khác nhau. Các giá trị sẽ khác nhau. – xmen

0

thử 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_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/routes_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5"> 
    </ListView> 

    <TableRow 
     android:id="@+id/toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:padding="0dp" > 

     <Button 
      android:id="@+id/btn_routes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/routes" 
      android:textSize="10dp" /> 

     <Button 
      android:id="@+id/btn_places" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="10dp" 
      android:text="@string/places" /> 

     <Button 
      android:id="@+id/btn_events" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="10dp" 
      android:text="@string/events" /> 

     <Button 
      android:id="@+id/btn_about" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/about" 
      android:textSize="10dp" /> 

    </TableRow> 

</LinearLayout> 
0
I won't suggest to go for -ve margins.. 

Thay thiết lập một số hình nền cho nút.

Một số điểm quan trọng: -

  1. Hình nền bản thân không được chứa bất kỳ khoảng trắng ở biên giới.
  2. Bạn phải tạo tệp bộ chọn cho nút đó sau khi cung cấp nền, trong đó bạn sẽ mô tả hình ảnh nào hiển thị ở các trạng thái khác nhau của nút tương tự như vậy selected, focused v.v. khác khôn ngoan bạn sẽ không thể thấy các hiệu ứng xuất hiện theo mặc định (như nút bạc và khi nó được nhấp nó được cam)

Giải pháp: -

  1. Làm thế nào để tạo ra một bộ chọn bạn có thể tham khảo this lik
  2. .210
  3. Tôi đã sử dụng this button in my case

My Kết quả: -

enter image description here

-2

Thêm hai dòng này trong tất cả các nút và sử dụng tuyến tính bố trí thay vì TableLayout với định hướng ngang.

android:layout_width="0dp" 
android:button_weight="1" 
19

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"

+1

Điều này thực sự phải là câu trả lời được chấp nhận. Nó không giống như có một lề thực sự giữa các nút và các nút khác, chỉ là nền được áp dụng có một số đệm trên đó. – CodyEngel

+0

Wow, thực sự .. Tôi đã dành khá nhiều thời gian để tìm ra lý do tại sao tôi không thể loại bỏ không gian/lề giữa các nút trong Gridview của tôi, thay đổi nền như bạn đã chỉ cho tôi, rằng không có bất kỳ không gian/lề nào ở giữa chúng nữa, cảm ơn câu trả lời này. –

+1

Các nút sử dụng bản vá 9 có thể vẽ được khiến cho nó trông giống như có thêm một lề xung quanh nó. –

0

đơn giản, Bạn có thể làm điều đó bằng cách ghi đè các nền mặc định với có hình dạng tròn và margin:

<Button 
      android:id="@+id/button_1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      <!--Replace the following color with any other color or drawable--> 
      android:background="@android:color/white" 
      android:text="@string/button1_text" 
      android:textColor="@android:color/black" /> 
Các vấn đề liên quan