The first top view / overlap of the second in LinearLayout

Is it possible to show the first view in LinearLayout, overlapping the second?

I would like my views to look like this:

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentRight="true" > <TextView android:id="@+id/firstTextView" android:layout_width="wrap_content" android:layout_height="wrapContent" /> <TextView android:id="@+id/secondTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

But I need my first view from the layout, firstTextView, to be placed on top of (overlapping) secondTextView. Is it possible? I use LinearLayout because I also play with margins to get an overlapping effect.

+6
source share
4 answers

What worked for me and probably works for you is that:

  • Wrap 2 TextViews with RelativeLayout instead of LinearLayout and set android: clipChildren = "false". This will prevent the part of the floor from being cut off.
  • Layout of the second TextView under the first TextView
  • In code, call the showToFront () method on the first TextView. By default, the first text image is drawn first and will be below the second text field. A call to the bringToFront () method will change this order.

Thus, the layout may be something like this:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:clipChildren="false"> <TextView android:id="@+id/firstTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:text="First View" /> <TextView android:id="@+id/secondTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/firstTextView" android:background="#00000000" android:layout_marginTop="-13dp" android:text="Second View"/> </RelativeLayout> 

and

 TextView firstTextView = (TextView)findViewById(R.id.firstTextView); firstTextView.bringToFront(); 
+3
source

If everything you want should overlap two views vertically, then use this XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/firstTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:text="First View" /> <TextView android:id="@+id/secondTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:layout_marginTop="-13dp" android:text="Second View"/> </LinearLayout> 

enter image description here

+3
source

Or you can stick with Linear Layout, but put a RelativeLayout inside it as a child. You can put your TextViews in RelativeLayout so that they do not transfer properties from RelativeLayout. Then you can use LinearLayout for other views. http://developer.android.com/reference/android/widget/RelativeLayout.html

+2
source

Assigning the value of the field "<0", we can overlap the view. But relative layout is preferable when we need o types of overlap.

0
source

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


All Articles