Failed to align views

This is part of a very long layout code. So publishing all the code is useless.
I want to align the RED circle. Both positions are displayed in red circles.
ParentLayout all activity is RelativeLayout , but it has nothing to do with it.
Please help me. I tried everything I knew.

enter image description here

 <LinearLayout android:id="@+id/thumbnail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/texttitle" android:orientation="horizontal" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/Layoutborder" > <LinearLayout android:id="@+id/Linearimagetop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dip" android:layout_alignParentLeft="true" android:background="@drawable/image_bg" > <ImageView android:id="@+id/productimage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/defaultimage" android:layout_below="@+id/texttitle" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Availability " android:textColor="@color/black" android:textStyle="normal" /> <View android:layout_width="1dp" android:layout_height="fill_parent" android:background="@color/black" android:layout_marginLeft="3dp" android:layout_marginRight="3dp"/> <TextView android:id="@+id/availibility" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="In Stock" android:textColor="#4A7023" android:textStyle="normal" android:layout_marginLeft="3dp"/> </LinearLayout> </LinearLayout> 
+6
source share
4 answers

Change the third LinearLayout to:

 <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:padding="5dp" > 

Not sure if you are comfortable with this layout.

+1
source

The code you submitted does not show where you specified the properties of the RED text "here." Perhaps you could just add it as another element in your relative layout, where you specify the availability and stock that would align it with the previous text.

Edit: (after understanding your actual problem)

I would suggest wrapping it in another linear layout that will allow you to place text above the image, as shown in the code below.

 <?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="100dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="20dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Availability " android:textStyle="normal" /> <View android:layout_width="1dp" android:layout_height="fill_parent" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" /> <TextView android:id="@+id/availibility" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="3dp" android:text="In Stock" android:textColor="#4A7023" android:textStyle="normal" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/thumbnail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/texttitle" android:layout_marginBottom="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <LinearLayout android:id="@+id/Linearimagetop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:padding="3dip" > <ImageView android:id="@+id/productimage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/texttitle" android:src="@drawable/button" /> </LinearLayout> </LinearLayout> </LinearLayout> 

+1
source

If all this is inside a RelativeLayout, remove the external LinearLayout (moving fields, etc. to the first descendant of LinearLayout as needed) and change the second child LinearLayout - the one that has 5dp padding, so that

 android:layout_alignParentRight="true" android:layout_alignParentTop="true" 

If you do not want to do this, and for some reason you need to leave an external LinearLayout, move

 android:layout_marginTop="20dp" 

from the parent LinearLayout to the first child and set the layout_width of the second child fill_parent and add android: gravity = "right" to it.

+1
source

This was another way to achieve the above result. Hope this can help SomeOne.

 <RelativeLayout android:id="@+id/thumbnail" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/white" > <LinearLayout android:id="@+id/Linearimagetop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dip" android:background="@drawable/image_bg" android:layout_alignParentLeft="true" > <ImageView android:id="@+id/productimage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/defaultimage" android:layout_below="@+id/texttitle" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Availability " android:textColor="@color/black" android:textStyle="normal" /> <View android:layout_width="1dp" android:layout_height="fill_parent" android:background="@color/black" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" /> <TextView android:id="@+id/availibility" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="In Stock" android:textColor="#4A7023" android:textStyle="normal" android:layout_marginLeft="3dp" /> </LinearLayout> </RelativeLayout> 
0
source

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


All Articles