Ajay Kulkarni
Ajay Kulkarni

Reputation: 3039

Dividing and positioning custom layouts equally in android relative layout

This is continuation from my previous question. Layout is changed as given below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black_70_opacity"
    android:weightSum="100"
    tools:context="com.dell.eventapp.ui.activity.HomeActivity">

    <LinearLayout
        android:id="@+id/top_image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/img_height"
        android:background="@drawable/activity_background"
        android:contentDescription="@string/top_image"
        android:orientation="horizontal"
        android:scaleType="centerCrop"
        android:weightSum="25">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:paddingBottom="@dimen/margin_8dp"
            android:text="@string/cio_summit_monaco_2015"
            android:textColor="@android:color/white"
            android:textSize="@dimen/large_font" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_image"
        android:layout_below="@id/top_image"
        android:orientation="vertical"
        android:weightSum="65">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="160sp"
            android:layout_marginStart="@dimen/margin_20dp"
            android:layout_marginTop="@dimen/margin_30dp"
            android:orientation="horizontal"
            android:weightSum="2">

            <TextView
                android:id="@+id/agenda"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="@dimen/margin_20dp"
                android:layout_weight="1"
                android:background="@drawable/activity_background"
                android:gravity="center|bottom"
                android:paddingBottom="@dimen/margin_10dp"
                android:text="@string/agenda"
                android:textColor="@android:color/white"
                android:textSize="@dimen/medium_font" />

            <TextView
                android:id="@+id/sessions"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="@dimen/margin_20dp"
                android:layout_weight="1"
                android:background="@drawable/activity_background"
                android:gravity="center|bottom"
                android:paddingBottom="@dimen/margin_10dp"
                android:text="@string/sessions"
                android:textColor="@android:color/white"
                android:textSize="@dimen/medium_font" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="160sp"
            android:layout_marginStart="@dimen/margin_20dp"
            android:layout_marginTop="@dimen/margin_30dp"
            android:orientation="horizontal"
            android:weightSum="2">

            <TextView
                android:id="@+id/speaker"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginRight="@dimen/margin_20dp"
                android:layout_weight="1"
                android:background="@drawable/activity_background"
                android:gravity="center|bottom"
                android:paddingBottom="@dimen/margin_10dp"
                android:text="@string/speakerbio"
                android:textColor="@android:color/white"
                android:textSize="@dimen/medium_font" />

            <TextView
                android:id="@+id/venues"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/margin_20dp"
                android:layout_weight="1"
                android:background="@drawable/activity_background"
                android:gravity="center|bottom"
                android:layout_gravity="center"
                android:paddingBottom="@dimen/margin_10dp"
                android:text="@string/venues"
                android:textColor="@android:color/white"
                android:textSize="@dimen/medium_font" />

        </LinearLayout>

    </LinearLayout>


    <RelativeLayout
        android:id="@+id/bottom_image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/img_height1"
        android:layout_alignParentBottom="true"
        android:background="@drawable/activity_background"
        android:contentDescription="@string/top_image"
        android:scaleType="centerCrop"
        android:weightSum="10">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:paddingBottom="@dimen/margin_8dp"
            android:text="@string/home_content"
            android:textColor="@android:color/white"
            android:textSize="@dimen/large_font" />

    </RelativeLayout>

</RelativeLayout>  

In root Relativelayout, I've used android:weightSum="100", in top Linearlayout, I've used android:weightSum="25". In 2nd LinearLayout, I've used android:weightSum="65". In nested LinearLayout, I've used android:weightSum="2". In 3rd LinearLayout, I've used android:weightSum="2". In bottom RelativeLayout, I've used android:weightSum="10". Resulting UI is as given below:
UI
Custom layouts used aren't spaced evenly at top and bottom. How can I do that by using android:weightsum and android:layout_weight?

Upvotes: 1

Views: 99

Answers (1)

Prafulla Malviya
Prafulla Malviya

Reputation: 396

Please have a look. I hope it will work for you.

<?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">

    <ImageView
        android:id="@+id/imageview_1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:background="#33000000"
        android:src="@android:drawable/ic_menu_gallery" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageview_1"
        android:gravity="center"
        android:text="CIO Summit Monaco 2015"
        android:textColor="#000000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/imageview_2"
        android:layout_below="@+id/imageview_1"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview_11"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


            <ImageView
                android:id="@+id/imageview_12"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview_21"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


            <ImageView
                android:id="@+id/imageview_22"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="#33000000"
                android:src="@android:drawable/ic_menu_gallery" />


        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageview_2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="#33000000"
        android:src="@android:drawable/ic_menu_gallery" />
</RelativeLayout>

Upvotes: 2

Related Questions