【问题标题】:How to add margin to aligned component in Android如何在Android中为对齐的组件添加边距
【发布时间】:2013-07-18 16:07:33
【问题描述】:

我使用带有一些小部件的 RelativeLayout。我有一个 RadioGroup 和一个 TextView,我希望 textview 与 radiogroup 对齐,并为其添加一个边距,使其比 radiogroup 稍微靠内。

这是我的代码:

<RadioGroup
        android:id="@+id/radiogroupSync"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/labelTitle"
        android:layout_marginLeft="10dp">

      <RadioButton
          android:id="@+id/radioFull"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/lblFull"
          />

      <RadioButton
          android:id="@+id/radioPart"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/lblPart" />
    </RadioGroup>
 <TextView
        android:id="@+id/lblData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/radiogroupSync"
        android:layout_below="@+id/radiogroupSync"
        android:layout_margin="3dp"
        android:singleLine="true"
        android:text="2010.01.01."
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginLeft="100dp"
    />

很明显,TextView 的边距不是 100dp,即使我删除了 alignLeft。如何设置边距?

【问题讨论】:

    标签: android android-layout margin


    【解决方案1】:

    边距不适用于对齐。您可以改用左填充。

    有点离题,但我不建议在 dp 中为边距/填充使用奇数 - 原因是,例如,为 hdpi 缩放的 3dp 将是 4.5 像素,这在某些情况下会出现问题。

    【讨论】:

    • 是否有意忽略边距?我不使用奇数,我总是使用偶数(参见 100dp 或 16 sp)请求澄清或提出改进建议。
    • 我在你的 中指的是这一行:android:layout_margin="3dp" 我不确定这是否是故意的,但我猜 align 的实现设置了与边距设置,只有更高的优先级。
    猜你喜欢
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多