2012-02-26 134 views
13

我需要在Android應用程序中創建自己的工具欄。 現在看起來像這樣:enter image description here刪除LinearLayout中的按鈕間距(Android)

所以你看到按鈕之間的空間。我試圖在按鈕上放置負邊距/填充,但空間並未消失。

這裏是佈局代碼:

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

如何按鈕之間移除空間?

+0

也許你可以檢查此 - HTTP:/ /stackoverflow.com/questions/2277732/how-to-get-rid-of-the-extra-gap-between-a-button-and-other-views – 2012-02-26 09:32:28

+0

這個想法是使用具有相同背景色的ImageButtons。 – hovanessyan 2012-02-26 09:37:06

回答

8

這裏有一個解決方案。我似乎急忙在這裏發表一個問題。

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

結果: enter image description here

+6

我不認爲這會適用於所有設備。 – 2013-01-01 19:06:04

+0

不好的答案,用不同的按鈕樣式。這些值將會不同。 – xmen 2015-04-29 06:03:47

0

試試這個

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

而是設置爲按鈕一些背景圖像。

一些要點: -

  1. 背景圖像本身不應該包含在邊界任何空白。
  2. 在提供背景後,您必須爲該按鈕創建一個選擇器文件,其中您將描述在不同狀態的按鈕中顯示哪個圖像,例如selectedfocused等。其他方面,您將無法看到效果默認顯示(如銀按鈕,點擊它時,它得到橙色)

解決方案: -

  1. 如何創建一個選擇,你可以參考this lik
  2. 我用this button in my case

我的結果: -

enter image description here

-2

添加這兩條線中的所有按鈕,使用線性佈局,而不是tablelayout與水平方向。

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

試着改變按鈕的顏色,因爲按鈕是原產於Android系統的默認界面實際上比它的大小更小,它的中心安裝,使它看起來很酷。

將它的背景更改爲黑色或其他內容,您將看到按鈕的實際大小。

的android:背景= 「#000」

+1

這應該是被接受的答案。它看起來不像按鈕和其他按鈕之間有實際的邊距,只是應用的背景有一些填充。 – CodyEngel 2015-08-04 01:52:20

+0

哇,真的..我花了相當長的一段時間搞清楚爲什麼我不能擺脫GridView中按鈕之間的空間/邊距,改變了你說的背景,告訴我,真的沒有任何空間/餘量在他們之間了,謝謝你的回答。 – 2015-10-18 14:05:48

+1

按鈕使用9補丁Drawable,使它看起來像它周圍有一個額外的餘量。 – 2015-12-11 04:41:53

0

簡單地說,你可以通過重寫默認的背景做它有一個圓形的形狀和保證金:

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