[英]Weird margin issue with relative layout
I am trying to have a TextView to fill the space between a button on the left and the edge of the layout on the right. 我试图有一个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!" />
What I am seeing in the editor however is android:layout_marginRight
is applied on the left. 我在编辑器中看到的是
android:layout_marginRight
应用于左侧。 The end result is 40dp margin on the left and 0 margin on the right. 最终结果是左边为40dp裕度,右边为0裕度。
It's the following attributes that don't quite work together as you'd expect: 以下属性不能像您期望的那样协同工作:
android:layout_toRightOf="@id/setInfusionReminder"
android:layout_alignParentRight="true"
Sidenote: Atleast from what I've seen in my own projects, it tends to make the element somewhat uncontrollable for further layout-related attributes. 旁注:与我在自己的项目中所看到的完全一样,它倾向于使该元素对于与布局相关的其他属性有些不可控制。
Update to address the comment: 更新以解决评论:
I'd probably do the following: 我可能会执行以下操作:
1. Remove the android:layout_alignParentRight="true"
2. Set the android:layout_width to "fill_parent"
3. Keep the gravity at android:gravity => "center"
This should make it expand as much as possible to the right, while centering the content (text). 这应该使它尽可能向右扩展,同时使内容(文本)居中。
I think you using both android:layout_alignParentRight="true"
and android:layout_toRightOf="@+id/setInfusionReminder"
. 我认为您同时使用了
android:layout_alignParentRight="true"
和android:layout_toRightOf="@+id/setInfusionReminder"
。 Remove the android:layout_alignParentRight
and try it should work as you like. 删除
android:layout_alignParentRight
并尝试按照您的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!" />
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.