Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of the extra gap between a button and other views?

Tags:

android

When I created a button view, Android always create some extra space between this button and other views below it.

In this example below, there is a button above the second button. You can see the gap between these two buttons. How can I get rid of this gap? Thanks.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button android:id="@+id/up"
        android:layout_width="fill_parent" android:layout_height="28dip"
        android:textSize="9sp" android:gravity="center" android:text="Hide sth"
        android:layout_marginBottom="0" android:paddingBottom="0" />
    <Button android:id="@+id/down"
        android:layout_width="fill_parent" android:layout_height="28dip"
        android:textSize="9sp" android:gravity="center" android:text="Show sth" />
   </LinearLayout>
like image 775
user256239 Avatar asked Feb 17 '10 01:02

user256239


People also ask

How do you get rid of gaps between buttons?

Browsers always add spaces between some elements, including buttons. To remove these, you need to set font-size to zero for their parent container. Then, to restore text size inside buttons, set font-size for them.

How do I put space between buttons in HTML?

How do I add a space between buttons? Add an Empty div Element Between Two Buttons to Add Space Between Them in HTML. Use the margin-right Property to Put Space Between the Buttons in HTML. Use the margin-right Property and not(:last-child) Selector to Provide Space Between Multiple Buttons in HTML.

How do I keep the button on the bottom of my screen Android?

Make the Parent of the LinearLayout containing the button as relative and set the property of linear layout containing two buttons as android:layout_alignParentBottom="true". You can also set the gravity of the Linear Layout to Bottom. Save this answer.


2 Answers

Create your own button background nine-patch PNG that eliminates the space. You can find the existing background files in your SDK.

like image 105
CommonsWare Avatar answered Oct 11 '22 11:10

CommonsWare


CommonsWare pretty much pointed you in the right direction but here's a sample layout example. Just replace the android:background="COLOR" to a reference of a 9 patch png


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<Button
    android:id="@+id/btn1"
    android:text="btn1"
    android:padding="0dip"
    android:layout_margin="0dip"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:background="#FFF"
/>

<Button
    android:id="@+id/btn2"
    android:text="btn2"
    android:padding="0dip"
    android:layout_margin="0dip"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:background="#FF0000"
/>

</LinearLayout>

Hope that helps!

like image 39
jagsaund Avatar answered Oct 11 '22 10:10

jagsaund