如何在Android中为对齐的组件添加边距

3
我使用RelativeLayout布局并放置了一些小部件。我有一个RadioGroup和一个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"
    />

很明显,即使我删除alignLeft,TextView也不会被100dp边距包围。我该如何为它设置边距?
1个回答

4

Margin和Align不兼容。您可以使用左填充代替。

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


忽略边距是有意为之吗?我不使用奇数,总是使用偶数(例如100dp或16sp)。请请求澄清或建议改进。 - Nestor
我指的是你的<TextView/>中的这一行: android:layout_margin="3dp"我不确定它是否是有意为之的,但我猜align的实现设置了与margins相同的值,只是具有更高的优先级。 - npace

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接