Question

I have a FrameLayout with many children. One of the children is a LinearLayout. I want the width of the LinearLayout to match_parent but about 90% so; which means I want to add some sort of paddingLeft/Right or margingLeft/Right. But when I add the padding or margin, it is not applied in the Graphical Layout. How do I do this correctly?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:background="@color/red"
        android:marginLeft="20dp"
        android:marginRight="20dp"
        android:orientation="vertical" >
....
Was it helpful?

Solution

you have

android:marginLeft="20dp"
android:marginRight="20dp"

instead change it to:

android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"

If it still doesnot work, put this layout inside another layout and set the margins to the other layout.

OTHER TIPS

You worte following code, which is not correct

android:marginLeft="20dp"
android:marginRight="20dp"

Try This :-

  <?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="match_parent"
        android:background="@android:color/black"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            **android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"**
            android:background="@android:color/white"
            android:orientation="vertical" >
        </LinearLayout>

    </FrameLayout>

Similarly you can give margin at Top and Bottom

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top