Domanda

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?

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top