【问题标题】:Remove space between buttons in LinearLayout (Android)删除 LinearLayout (Android) 中按钮之间的空间
【发布时间】:2012-03-16 03:45:16
【问题描述】:

我需要在 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>

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

【问题讨论】:

标签: android toolbar margin android-linearlayout


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    这里有一个解决方案。好像急着发个问题来这里。

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

    结果:

    【讨论】:

    • 不好的答案,不同的按钮样式。值会有所不同。
    【解决方案3】:
    I won't suggest to go for -ve margins..
    

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

    一些要点:-

    1. 背景图片本身不应在边框处包含任何空白。
    2. 您必须在提供背景后为该按钮创建一个选择器文件,在该文件中您将描述在按钮的不同状态下显示的图像selectedfocused 等。否则您将无法查看默认出现的效果(如银色按钮,点击后变为橙色)

    解决方案:-

    1. 如何创建选择器可以参考this lik
    2. 我用过this button in my case

    我的结果:-

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        尝试改变按钮的颜色,因为android原生的按钮的默认界面实际上比它的大小要小,而且它是居中的,让它看起来很酷。

        把它的背景改成黑色什么的,你就会看到按钮的真实大小了。

        android:background="#000"

        【讨论】:

        • 这确实应该是公认的答案。看起来按钮和其他按钮之间没有实际的边距,只是应用的背景上有一些填充。
        • 哇,真的......他们之间不再有任何空间/边距,感谢您的回答。
        • 按钮使用 9 补丁 Drawable,使它看起来像是有一个额外的边距。
        【解决方案6】:

        简单地说,您可以通过使用圆形和边距覆盖默认背景来做到这一点:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-11
          相关资源
          最近更新 更多