Layouts on top of each other

This is a matter of Android development.
If I first set the content view to my xml layout using setContentView(R.layout.main); and then add another content view using addContentView(layout, params) , the content views overlap with each other. I want the content in the second view to appear right below the first. Is it possible for me or do I need to somehow combine xml and programmatically created layouts? Both layouts are linear.

Thank you in advance

+1
source share
3 answers

Place both layouts inside the LinearLayout with a vertical orientation. It is he.

Edit:

what i mean, you need to create 3 xml files.

  • main.xml
  • layout1.xml -> your first layout
  • layout2.xml -> your second layout

your main shoul layout file will be like this:

 <LinearLayout android:orientation="vertical"> <include android:id="+id/lay1" android:layout="@layout/layout1"/> <include android:id="+id/lay2" android:layout="@layout/layout2"/> </LinearLayout> 

Now your main layout for your setContentView(R.layout.main)

+5
source

Thanks guys. I appreciate the feedback.

This did not help to make the orientation vertical on the parent layout (CoordinatorLayout).

What worked was to place both elements in a LinearLayout with a vertical orientation.

One comment above said that I should make the first appbarlayout layout: Yes, I changed this. I changed it to LinearLayout as one of my attempts to make objects stop drawing on top of each other.

I'm not sure if this was the right way to solve this problem, but it worked. I will continue. Thanks again.

+1
source

To place the layout on top of each other (one top under the other), this is what I did:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="match_parent"> <RelativeLayout android:id="@+id/topLayout" android:layout_width="match_parent" android:layout_height="300dp" android:layout_alignParentTop="true" android:background="@color/colorAccent"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="126dp" android:text="Existing Client" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="300dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="@color/colorPrimary"> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="138dp" android:text="New Client" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> 

And the result:

enter image description here

My intention was to use layouts as buttons that fill a specific width of the screen.

0
source

Source: https://habr.com/ru/post/1484790/


All Articles