删除LinearLayout(Android)中按钮之间的空格

时间:2012-02-26 09:30:31

标签: android toolbar margin android-linearlayout

我需要在Android应用程序中创建自己的工具栏。 现在它看起来像这样:enter image description here

所以你看到按钮之间的空格。我试图在按钮上放置负边距/填充,但空间没有消失。

这是布局代码:

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

如何删除按钮之间的空间?

6 个答案:

答案 0 :(得分:21)

尝试更改按钮的颜色,因为android原生的按钮的默认界面实际上小于它的大小,并且它适合中心以使其看起来很酷。 / p>

将背景更改为黑色或其他内容,您将看到该按钮的实际大小。

机器人:背景=&#34;#000&#34;

答案 1 :(得分:9)

这是一个解决方案。我似乎急忙在这里发一个问题。

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

答案 2 :(得分:1)

I won't suggest to go for -ve margins..

而是为按钮设置一些背景图像。

一些要点: -

  1. 背景图片本身不应包含边框处的任何空白区域。
  2. 您必须在提供背景后为该按钮创建一个选择器文件,您将在其中描述在不同按钮状态下显示的图像selectedfocused等等。无法看到默认显示的效果(如银色按钮,单击时会变为橙色)
  3. 解决方案: -

    1. 如何创建选择器,您可以参考this lik
    2. 我使用过this button in my case
    3. 我的结果: -

      enter image description here

答案 3 :(得分: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>

答案 4 :(得分: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" />

答案 5 :(得分:-2)

在所有按钮中添加这两行,并使用线性布局而不是水平方向的tablelayout。

android:layout_width="0dp" 
android:button_weight="1"
相关问题