I just found I cannot remove the space between 2 buttons even if I set the layout_marginRight
and layout_marginLeft
as below. But it make sense if I set the space larger such as 10 dp.
Any way to solve it?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal" android:padding="0dp"
android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp">
<Button android:id="@+id/LocationTitleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Add location"
android:textStyle="bold" />
<Button android:textColor="#FF000000"
android:layout_weight="0"
android:id="@+id/AddLocationButton"
android:text="Search"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp" />
</LinearLayout>
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
If you have a single Button in your Activity and you want to center it the simplest way is to use a RelativeLayout and set the android:layout_centerInParent=”true” property on the Button.
To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.
2 answers. In addition to the buttons you will need to use an view "empty" (1) to represent the spaces before, between and after each button. layout_weight attribute is used to scale the spaces equally.
Please look at Bryan's answer. With my answer both buttons overlap. Bryans answer shows the real size of the buttons.
Just set the android:layout_marginRight
of the first button to "-8dip"
or even more. Than the space between the two buttons will get smaller.
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