Android layout customization

I am starting android development and still have layout issues.

This is the render I want: render
(source: hostingpics.net )

I did it, but I really don't understand how I can make this type of layout.

This is my XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="90px"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView1" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="90px"
        android:id="@+id/linearLayout2">
        <TextView
            android:text="Bonus1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:layout_gravity="center" />
        <TextView
            android:text="Bonus2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView2"
            android:layout_gravity="center" />
    </LinearLayout>
</LinearLayout>

      

+3


source to share


3 answers


Use linear layouts




XML-CODE

::

<LinearLayout 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:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:padding="5dp"
        android:weightSum="2" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:padding="10dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="#000000"
                android:padding="5dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF"
                    android:gravity="bottom|center" >

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="2dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:text="TextView"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_weight="1" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:weightSum="2" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#FFFFFF"
                        android:orientation="vertical" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="5dp"
                            android:layout_weight="1"
                            android:background="#000000"
                            android:padding="5dp" >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="#FFFFFF"
                                android:gravity="bottom|center" >

                                <TextView
                                    android:id="@+id/textView2"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="2dp"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginRight="5dp"
                                    android:text="TextView"
                                    android:textStyle="bold" />
                            </LinearLayout>
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="5dp"
                            android:layout_weight="1"
                            android:background="#000000"
                            android:padding="5dp" >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="#FFFFFF"
                                android:gravity="bottom|center" >

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="2dp"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginRight="5dp"
                                    android:text="TextView"
                                    android:textStyle="bold" />
                            </LinearLayout>
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:gravity="bottom|center" >
    </LinearLayout>

</LinearLayout>

      




Snapshot

enter image description here




Note :: Use this code and customize the way you want

Hope it helps!

+1


source


Use Gridlayout for this approach

or

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TestActivity" >

    <com.example.testview.SquareView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@android:color/white"
        android:text="Largetext" />

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

        <com.example.testview.SquareView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:text="smallText" />

        <com.example.testview.SquareView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:padding="5dp"
            android:text="smallText" />
    </LinearLayout>

</LinearLayout>

      



custom square Textview

package com.example.testview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class SquareView extends TextView {

    public SquareView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SquareView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

}

      

0


source


We recommend using ConstrantLayout because ConstraintLayout allows you to create large and complex layouts with a flat presentation hierarchy. Learn more about ConstraintLayout here .

Solution using ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp">

        <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guide_line_1"
                android:layout_width="1dp"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.5" />

        <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guide_line_2"
                android:layout_width="1dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.5" />

        <TextView
                android:id="@+id/text_view_1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                android:ellipsize="end"
                android:text="@string/big_lorem"
                app:layout_constraintBottom_toBottomOf="@id/text_view_3"
                app:layout_constraintEnd_toEndOf="@id/guide_line_1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/text_view_2" />

        <TextView
                android:id="@+id/text_view_2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginBottom="8dp"
                android:text="@string/small_lorem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@id/guide_line_1"
                app:layout_constraintTop_toTopOf="parent" />

        <TextView
                android:id="@+id/text_view_3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="16dp"
                android:text="@string/small_lorem"
                app:layout_constraintBottom_toBottomOf="@id/guide_line_2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@id/guide_line_1"
                app:layout_constraintTop_toBottomOf="@id/text_view_2" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

      

The final layout looks like this in design mode.

Result

0


source







All Articles