开发者

Button in relative layout not affected by setting layout margins

A custom Dialog box with a RelativeLayout contains a Button widget that won't change its margins, regardless of direction. Here's the xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal" android:background="#99000000">
    <TextView android:id="@+id/dialogtitle" android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:text="Some text" />
    <TextView android:id="@+id/dialogtext" android:layout_width="300dp"
        android:layout_height="wrap_content" android:textSize="15dp"
        android:layout_alignParentLeft="true" android:layout_below="@id/dialogtitle"
        android:paddingLeft="8dp" /开发者_如何转开发>
    <Button android:id="@+id/dialogbuttoninfo" android:layout_width="80dp"
        android:layout_height="wrap_content" android:text="Doesn't care about margins"
        android:layout_alignParentRight="true" android:layout_marginLeft="128dp" />
</RelativeLayout>

Padding works but only moves the text inside the button. Any suggestions?


This seems crappy but have you tried putting it all in a LinearLayout instead? or perhaps removing android:orientation="horizontal". I dont think RelativeLayout cares about orientation from what I've seen. Also I think, but might be wrong, that if you do a LinearLayout then you won't need to have android:layout_alignParentLeft="true" in there either.

After cleaning it up a bit (sorry but its so hard to read when its compressed as it was in the question) you also didn't state where the last TextView , dialogbuttoninfo was supposed to be relative to everything else, I think you have to do that for Relative layouts to behave properly, I've had some squirrely things happen.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#99000000">
    <TextView
        android:id="@+id/dialogtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some text" />
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
        <TextView
            android:id="@+id/dialogtext"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_alignParentLeft="true"
            android:paddingLeft="8dp" />
        <Button
            android:id="@+id/dialogbuttoninfo"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="Doesn't care about margins"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="128dp" />
    </LinearLayout>
</LinearLayout>


You may need to align the left of the button against something before the margin has real meaning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜