Elyas Erfani
Elyas Erfani

Reputation: 19

how to use 50% width and height in android linear layout

I want to create android kind of layout like this :

IMAGE

I created same this with relative layout but I can't place second container with 50% width and height.

This is my code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">


    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:weightSum="100">

            <ImageView
                android:layout_margin="5dp"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:adjustViewBounds="true"
                android:layout_height="wrap_content"
                android:src="@drawable/akhbar"
                android:id="@+id/akhbar_panel" />

            <ImageView
                android:id="@+id/amozesh_panel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="50"
                android:adjustViewBounds="true"
                android:src="@drawable/amozesh" />

        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

Upvotes: 0

Views: 2297

Answers (2)

Ben P.
Ben P.

Reputation: 54194

Here's a way to solve it using ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#fac"
        android:text="one"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/two"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#afc"
        android:text="two"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/one"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/three"/>

    <TextView
        android:id="@+id/three"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#caf"
        android:text="three"
        app:layout_constraintTop_toBottomOf="@+id/two"
        app:layout_constraintLeft_toRightOf="@+id/one"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

Upvotes: 1

Ben P.
Ben P.

Reputation: 54194

I think a better parent for this view would be ConstraintLayout, but if you have to use LinearLayout then you can do this:

<?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="200dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#fac"
        android:text="one"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#afc"
            android:text="two"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#caf"
            android:text="three"/>

    </LinearLayout>

</LinearLayout>

enter image description here

Upvotes: 3

Related Questions