【问题标题】:Cannot change button's layout_margin from styles?无法从样式更改按钮的 layout_margin?
【发布时间】:2014-04-05 13:01:53
【问题描述】:

我正在尝试设置所有按钮的样式,但由于某种原因,我的样式并未影响它们的边距。 (它们紧挨着出现)但是在 layout.xml 上更改 layout_margin 确实有效。

这是我的代码:

<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>

这是我的布局:

<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>
//...

我做错了什么?

【问题讨论】:

  • 请将 layout.xml 添加到您的问题中。
  • 我刚刚添加了它。谢谢
  • 在这种情况下,如果您将所有按钮放在水平线性布局中,问题将得到解决,但我遇到过类似的奇怪问题,TableLayout 尝试使用另一个布局来实现它。
  • @Liso22:您是否将您的样式分配给主题中的 buttonStyle 属性?类似 @style/Button

标签: android xml styles


【解决方案1】:

我刚刚遇到同样的问题,使用边距的解决方案是在每个布局中定义每个按钮的样式,如下所示:

<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>
//...

因此,在这种情况下,您甚至不需要将其添加到主题中。

来源:https://stackoverflow.com/a/13365288/3564556

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2019-10-15
    • 2016-09-11
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多