Home > Software design >  Creating a complex View in Android
Creating a complex View in Android

Time:11-28

I am new to Android, and still learning it. I understood the difference between View and ViewGroup, and also found that a ViewGroup can contain multiple views, but a View cannot contain more Views inside it.

What I want to achieve is something like this: Final Image

And what I have right now is just a black box in the background. The XML for the black-box is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="400dp"
    android:layout_height="200dp"
    android:gravity="center"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical"
>

  <TextView
      android:id="@ id/simpleTextView"
      android:layout_width="400dp"
      android:layout_height="200dp"
      android:background="@drawable/box"
      android:text="Sample"
      android:textSize="20sp"
      android:gravity="center_horizontal"/>
</LinearLayout>

Where I have defined box in a seperate XML. I just wanted to ask, can anyone please help me in achieving the Final Image above? Especially, what kind of layout should I be using?

CodePudding user response:

You should make your layouts as flat as possible, I mean you should avoid nesting, for that in most cases use ConstraintLayout. You can read about it here: https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout and there are tons of tutorials about that.

CodePudding user response:

It not difficute but we recommend ConstraintLayout

<LinearLayout>//Horizontal
    <LinearLayout>//Vertical
        <TextView/>
        <Button/>
    </LinearLayout>
    <LinearLayout>//Vertical and add Margin
        <Button/>// add Margin
    </LinearLayout>
</LinearLayout>
  • Related