scientiffic scientiffic - 1 month ago 15
Android Question

Percentage width for a relative layout

I have a layout that I'd like to take up only a percentage of the screen's width (image on the left). Right now, my relative layout is spanning the entire width of the display (image on the right). How can I fix it to have margins on the left and right?

enter image description here

This is how I've defined my relative layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/white"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

Answer

for this you can use LinearLayout with weightSum and layout_wright property

so try following code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="8" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- 1/8 % of Screen to left of your RelativeLayour -->
    </LinearLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="6"
        android:background="@color/white"
        android:orientation="vertical" >
    </RelativeLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- 1/8 % of Screen to right of your RelativeLayour -->

    </LinearLayout>

</LinearLayout>
Comments