1

I'm trying to style all my buttons, however for some reason my style is not affecting their margins. (they appear right next to each other) Changing the layout_margin right on the layout.xml however does work.

This is my code:

<style name="Button" parent="@android:style/Widget.DeviceDefault.Button">
    <item name="android:layout_margin">5dp</item>
    <item name="android:background">@android:color/holo_orange_light</item>
</style>

This is my layout:

<TableRow
    android:layout_weight="1"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:clickable="false"
    android:layout_height="fill_parent"
    android:layout_width="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="1"
        android:id="@+id/button1"
        android:layout_weight="0.06"
        android:clickable="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="2"
        android:id="@+id/button2"
        android:layout_weight="0.06" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="3"
        android:id="@+id/button3"
        android:layout_weight="0.06"
        android:layout_marginBottom="2dp" // This works
        android:layout_marginTop="2dp" />

</TableRow>
//...

What am I doing wrong?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • please add the layout.xml to your question. – sadegh saati Apr 05 '14 at 14:22
  • in this case I thing if you put all buttons in horizontal linear layout problem would be solved,but I've had similar strange problem with TableLayout try to implement it using another Layout. – sadegh saati Apr 05 '14 at 14:46
  • @Liso22: Do you assign yout style to buttonStyle attribute in theme? Something like @style/Button – Anderson Apr 28 '14 at 08:15

1 Answers1

0

I was just facing the same problem and the solution with margins is in defining style on every button in every layout like this:

<TableRow
    android:layout_weight="1"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:clickable="false"
    android:layout_height="fill_parent"
    android:layout_width="match_parent">

    <Button
        style="@style/Button"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="1"
        android:id="@+id/button1"
        android:layout_weight="0.06"
        android:clickable="true" />

    <Button
        style="@style/Button"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="2"
        android:id="@+id/button2"
        android:layout_weight="0.06" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="3"
        android:id="@+id/button3"
        android:layout_weight="0.06"
        android:layout_marginBottom="2dp" // This works
        android:layout_marginTop="2dp" />
</TableRow>
//...

So, you don't even need to add it to theme in this case.

Source: https://stackoverflow.com/a/13365288/3564556

Community
  • 1
  • 1
gabriel.gircenko
  • 123
  • 1
  • 10