简体   繁体   中英

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
    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. The end result is 40dp margin on the left and 0 margin on the right.

在此处输入图片说明

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" . Remove the android:layout_alignParentRight and try it should work as you like.

     <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM