Убрать пробел между кнопками в LinearLayout (Android) - PullRequest
13 голосов
/ 26 февраля 2012

Мне нужно сделать свою собственную панель инструментов в приложении 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 ]

22 голосов
/ 13 июля 2014

Попробуйте изменить цвет кнопки, потому что стандартный интерфейс кнопки, которая является родной для Android, на самом деле меньше ее размера, и он расположен по центру, чтобы она выглядела круто.

Измените фончерным или что-то, и вы увидите реальный размер кнопки.

android: background = "# 000"

9 голосов
/ 26 февраля 2012

Вот решение.Кажется, я поспешил разместить вопрос здесь.

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

1 голос
/ 29 июля 2013
I won't suggest to go for -ve margins..

Скорее установите фоновое изображение для кнопки.

Некоторые важные моменты: -

  1. Само фоновое изображение не должно содержать пробеловна границах.
  2. Вы должны создать файл селектора для этой кнопки после предоставления фона, в котором вы будете описывать, какое изображение отображать в различных состояниях кнопки, аналогично selected, focused и т. д. в других случаяхвы не сможете увидеть эффекты, которые появляются по умолчанию (например, серебряная кнопка, а при нажатии она становится оранжевой)

Решения: -

  1. Как создать селектор вы можете обратиться этот лайк
  2. Я использовал эту кнопку в моем случае

Мой результат: -

enter image description here

0 голосов
/ 31 марта 2015

Проще, Вы можете сделать это, переопределив фон по умолчанию с округлой формой и полем:

<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" />
0 голосов
/ 16 декабря 2013

Добавьте эти две строки во все кнопки и используйте линейное расположение вместо таблицы с горизонтальной ориентацией.

android:layout_width="0dp" 
android:button_weight="1"
0 голосов
/ 26 февраля 2012

попробуйте

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