Sergei Vasilenko
Sergei Vasilenko

Reputation: 2343

layout_marginLeft works as right on Android API < 11 when View in FrameLayout

When I use layout_marginLeft or set left margin from code it works as layout_marginRight. This behavior was seen when I placed View with layout_marginLeft in FrameLayout or in a root of a layout on Android API < 11.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:background="#77cc99"
        >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:background="#eebbaa">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Hello World, MyActivity"
                />
        </FrameLayout>

</FrameLayout>

Upvotes: 3

Views: 1097

Answers (1)

VAV
VAV

Reputation: 1896

By default android:layout_gravity attribute set to "left" for FrameLayout. If you want to use android:layout_marginLeft you should change it:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginLeft="50dp"
    android:background="#77cc99"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginLeft="50dp"
        android:background="#eebbaa" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity" />    
    </FrameLayout>
</FrameLayout>

Upvotes: 7

Related Questions