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>
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 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.
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.
Create your own button background nine-patch PNG that eliminates the space. You can find the existing background files in your SDK.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With