Layout Problem with Button Margin

Layout problem with button margin

Remember, android:layout_* attributes are LayoutParams. They are arguments to the parent and affect how the parent will perform layout on that view. You're specifying layout_margin attributes on your buttons, but they're getting ignored. Here's why:

Since LayoutParams are specific to the parent view type, you need to supply an instance of the correct parent type when you inflate layouts using a LayoutInflater or else layout_ attributes on the top-level view in the layout will be dropped. (The inflater would have no idea what type of LayoutParams to generate.)

Since buttonList is your intended parent for the button views, change your inflate line to this:

btn = (Button) layoutInflater.inflate(R.layout.button, buttonList, false);

Margin of buttons in linear layout

I do not really think they have a margin but it is related to the background of the button. Probably the default background of the button has a image like this one:

http://developer.android.com/guide/developing/tools/draw9patch.html

which includes fiction margins. Here you can find more info about 9-Patch.

http://developer.android.com/guide/topics/resources/drawable-resource.html

In my opinion if you want to remove the "margins", you should create a different background for the image because the -3 value is not a good workaround (IHMO).

Trouble with margins for Button

activity1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/HeaderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:layout_marginTop="20dip"
android:fontFamily="sans-serif-light"
android:text="Performance informatie"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="normal|bold" />

<TextView
android:id="@+id/ValueTextView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:layout_marginTop="10dip"
android:text="-p789t89549"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
android:id="@+id/getValueButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset Alarm"
android:layout_gravity="bottom"/>

</LinearLayout>

activity2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:orientation="vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Commando "
android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/spinneritems"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Weger"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cel"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text "
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gewicht"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text "
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tolerantie"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

<Button
android:id="@+id/share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text=" Uitvoeren" />

</LinearLayout>

Now this layout will include both and will act as your main layout.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<include layout="@layout/activity1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>

<include layout="@layout/activity2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>

</LinearLayout>

Align buttons with equal margin in linear layout

You can wrap each of your button view in a parent layout say for ex. FrameLayout
and then you can use your weightSum Technique to get equal margins between each button. as in this case your button's size will remain constant and the parent layout's size will increase.So I think it can be solve your case.

How to center a button with margin in android layout?

I would remove the first LinearLayout, and just leave the button itself centered horizontally on screen and with some margin (I assumed the root element is a LinearLayout):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/list_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/ok"
android:layout_gravity="center_horizontal"
>
</Button>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/header_list"
android:layout_marginTop="36dp">

<Button
android:id="@+id/listname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.225"
android:layout_below="@id/list_ok"
android:text="@string/headerName">
</Button>
<Button
android:id="@+id/listok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/headerOK">
</Button>
<Button
android:id="@+id/listnok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/headerNOK">
</Button>
<Button
android:id="@+id/listhist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="@string/headerHist">
</Button>
<Button
android:id="@+id/listprandom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:text="@string/headerP">
</Button>
<Button
android:id="@+id/listphist"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.075"
android:text="@string/headerH">
</Button>
<Button
android:id="@+id/listptot"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.075"
android:text="@string/headerProb">
</Button>
<Button
android:id="@+id/listproc"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.075"
android:text="%">
</Button>

</LinearLayout>

<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_below="@id/header_list"
>
</ListView>

</LinearLayout>

If you want to keep your first LinearLayout remove the attribute layout_gravity from it and add android:layout_gravity="center_horizontal" to the button to center it horizontally.

EDIT:

If you use a RelativeLayout as the root element, you have to specify each children positioning. You forgot to position your "header_list" below your top row:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/top_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
>

<Button
android:id="@+id/list_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/ok"
android:onClick="buttonEndStat"
>
</Button>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/header_list"
android:layout_below="@id/top_row"
>

<Button
android:id="@+id/listname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.225"
android:layout_below="@id/list_ok"
android:text="@string/headerName">
</Button>
<Button
android:id="@+id/listok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/headerOK">
</Button>
<Button
android:id="@+id/listnok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="@string/headerNOK">
</Button>
<Button
android:id="@+id/listhist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="@string/headerHist">
</Button>
<Button
android:id="@+id/listprandom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:text="@string/headerP">
</Button>
<Button
android:id="@+id/listphist"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.075"
android:text="@string/headerH">
</Button>
<Button
android:id="@+id/listptot"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.075"
android:text="@string/headerProb">
</Button>
<Button
android:id="@+id/listproc"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.075"
android:text="%">
</Button>

</LinearLayout>

<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_below="@id/header_list"
>
</ListView>

</RelativeLayout>

To center your button horizontally, add android:gravity="center_horizontal" to its parent.

Button not auto margin when set background

just set margin with button like

android:layout_margin="10dp"

It will take all margin from all side :)



Related Topics



Leave a reply



Submit