2012-02-26 140 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" />