What is android:layout_alignLeft in Android?
Solution 1
layout_alignLeft
makes the left edge of your view match the left edge of the view whose ID you use as the parameters.
layout_toLeftOf
makes your view placed to the left side of the view whose ID you use (the right edge will be aligned with the left edge of the other view).
Solution 2
ALIGN_LEFT Rule that aligns a child's left edge with another child's left edge.
LEFT_OF Rule that aligns a child's right edge with another child's left edge.
in your above example the align left is used in a way below.
tv5 textview is align left of the tv3 layout in android.
Solution 3
android:layout_alignLeft aligns both left sides. (pic 1)
android:layout_toLeftOf aligns one layout to the left of another. (pic 2)
<LinearLayout 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"
android:background="#D6D6D6"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_alignLeft="@+id/ll1"
android:background="#00FF00"
android:orientation="vertical"> </LinearLayout>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="200dp"
android:layout_alignParentRight="true"
android:layout_height="200dp"
android:background="#00FFFF"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_toLeftOf="@+id/ll2"
android:background="#00FF00"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="200px"
android:layout_height="200px"
android:layout_alignParentRight="true"
android:background="#00FFFF"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Solution 4
Both parameters are used in relative layout. Talking about android:layout_Leftof
, this parameter makes your view to be placed "to the left" (not align) of view whose id you are passing as a value.On the other hand android:layout_alignLeft
will place you view starting from the same point(from left in this case) where the view your have passed the id is starting. Also int this case, your view may overlap the other view (as sometimes it is used to keep a view inside another view)
Rajesh Lawrance
Updated on July 06, 2022Comments
-
Rajesh Lawrance almost 2 years
i couldnt understand the attribute android:layout_alignLeft .i am new to android development.
<RelativeLayout android:id="@+id/r5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/r4" > <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginTop="10dp" android:textSize="16sp" android:textColor="#663300" android:text="TextView" /> <TextView android:id="@+id/tv5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_alignLeft="@id/tv3" android:textSize="16sp" android:textColor="#663300" android:text="TextView" /> <TextView android:id="@+id/tv6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv5" android:layout_marginTop="10dp" android:textSize="16sp" android:textColor="#663300" android:text="TextView" /> </RelativeLayout>
anyone can explain clearly?what is the difference between android:layout_Leftof and android:layout_alignLeft?
-
Andrew S over 8 yearsNo resource identifier found for attribute 'layout_Leftof' in package 'android'.