2012-07-10 34 views
1

我想要一个TextView来填充左边的按钮和右边布局的边缘之间的空间。相对布局的怪异边距问题

<TextView 
    android:id="@+id/welcomeHeader" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
android:layout_alignParentRight="true" 
    android:layout_toRightOf="@+id/setInfusionReminder" 
    android:layout_marginRight="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="20dp" 
    android:gravity="center" 
    android:text="Welcome!" /> 

我在编辑器中看到的是android:layout_marginRight应用在左边。最终结果是左侧40dp余量和右侧0余量。

enter image description here

+0

您是否尝试过使用'android:layout_gravity =“center”'? – b3by 2012-07-10 14:35:36

回答

1

这是下面的属性不很一起工作,你会期望:

android:layout_toRightOf="@id/setInfusionReminder" 
android:layout_alignParentRight="true" 

旁注:ATLEAST从我在我自己的项目见过,也容易使元素在某种程度上无法控制,以适应更多与布局相关的属性。

更新来解决注释:

我可能会做到以下几点:

1. Remove the android:layout_alignParentRight="true" 
2. Set the android:layout_width to "fill_parent" 
3. Keep the gravity at android:gravity => "center" 

这应该使它尽可能向右展开,而中心内容(文本)。

+0

问题是为什么以及如何使其按预期工作? – 2012-07-10 14:48:05

+0

我已经更新了我上面的答案。 – ninetwozero 2012-07-10 14:52:00

+0

它现在按预期工作吗? :) – ninetwozero 2012-07-10 14:56:07

0

我想你同时使用了android:layout_alignParentRight="true"android:layout_toRightOf="@+id/setInfusionReminder"。删除android:layout_alignParentRight并尝试它应该如你所愿。

 <TextView 
       android:id="@+id/welcomeHeader" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_toRightOf="@+id/btnSettkoff" 
       android:layout_marginRight="20dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="20dp" 
       android:gravity="center" 
       android:text="Welcome!" /> 

希望它有帮助。

+0

我想将文本居中放置在按钮和右边之间的空白处。删除'android:layout_alignParentRight'会使文本粘在按钮上。 – 2012-07-10 14:44:17

+0

我更新了为我工作的答案(测试)..试试这个 – GoCrazy 2012-07-10 14:54:11