1

I created some custom button styles. All of the styling items are applied to buttons in the Activity that uses this style, except for layout_margin_top. I don't really understand what is different about this item, so if someone could let me know why, that would be awesome. Attached are two images. The incorrect image with no margin between buttons was rendered with layout_marginTop="4dp" on the style, and the correct image with margin between buttons was rendered with layout_marginTop="4dp" on each button in the layout.

What I tried

enter image description here

My ideal style.

<style name="ButtonStyle" parent="@android:style/Widget.Button">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@drawable/button</item>
    <item name="android:paddingTop">8dp</item>
    <item name="android:paddingBottom">8dp</item>
    <item name="android:paddingLeft">8dp</item>
    <item name="android:paddingRight">8dp</item>
    <item name="android:textSize">@dimen/text_medium_small</item>
    <item name="android:layout_marginTop">4dp</item>
</style>

My buttons inside a LinearLayout:

<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:onClick="logSignificantEvent"
        android:text="@string/button_trigger_event"/>
<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:onClick="logDay"
        android:text="@string/button_subtract_a_day"/>
<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:onClick="showChoice"
        android:text="@string/button_force_choice"/>
<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:onClick="showRating"
        android:text="@string/button_force_rating"/>

What worked

enter image description here

Style without margin.

<style name="ButtonStyle" parent="@android:style/Widget.Button">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@drawable/button</item>
    <item name="android:paddingTop">8dp</item>
    <item name="android:paddingBottom">8dp</item>
    <item name="android:paddingLeft">8dp</item>
    <item name="android:paddingRight">8dp</item>
    <item name="android:textSize">@dimen/text_medium_small</item>
</style>

I had to apply margins directly to every button.

<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginTop="4dp"
        android:onClick="logSignificantEvent"
        android:text="@string/button_trigger_event"/>
<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginTop="4dp"
        android:onClick="logDay"
        android:text="@string/button_subtract_a_day"/>
<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginTop="4dp"
        android:onClick="showChoice"
        android:text="@string/button_force_choice"/>
<Button android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginTop="4dp"
        android:onClick="showRating"
        android:text="@string/button_force_rating"/>

For reference, here is the theme I am using on this Activity:

<style name="MyTheme" parent="android:Theme.Holo.NoActionBar">
    <item name="android:buttonStyle">@style/ButtonStyle</item>
</style>
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • Any solution or explanation? – Anderson Apr 28 '14 at 08:23
  • 2
    To all with this issue. Take a look at: http://stackoverflow.com/a/13365288/1087411. For me, this is a clear case of design flaw in android framework, a buf if you will. – Anderson Apr 28 '14 at 19:15
  • Please take a look at this answer using inset that creates margin equivalent: http://stackoverflow.com/a/13749983/355456 – Hong Dec 12 '16 at 14:45

0 Answers0