相对布局的奇怪边际问题

时间:2012-07-10 14:31:20

标签: java android

我正在尝试使用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

2 个答案:

答案 0 :(得分:1)

以下属性并不像您期望的那样完全协同工作:

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

旁注:从我在自己的项目中看到的内容来看,它往往会使元素在某些方面与进一步的布局相关属性无法控制。

更新以解决评论:

我可能会做以下事情:

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

这应该使它尽可能地向右扩展,同时使内容(文本)居中。

答案 1 :(得分: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!" />

希望它有所帮助。

相关问题