How to center a LinearLayout in RelativeLayout?
12,295
When using the RelativeLayout
, you can use android:layout_centerInParent="true"
, which will do what it says, center it inside its parent.
Author by
Wawanopoulos Ertz
Updated on June 14, 2022Comments
-
Wawanopoulos Ertz almost 2 years
I would like to center a LinearLayout in a RelativeLayout but i have a problem, the margin-bottom stays attached to bottom. Here is the view :
http://nsa33.casimages.com/img/2013/03/12//130312103139937791.png
And here is the code of the layout :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/group_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" > <RelativeLayout android:id="@+id/carreGlobalInterieur" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@color/grisTresClair" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/border_deals" android:orientation="vertical" > <RelativeLayout android:id="@+id/GroupLigneOne" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/grisClair" android:orientation="vertical" > <TextView android:id="@+id/CategorieDeal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="My Category" android:textColor="@color/white" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" > <TextView android:id="@+id/nouveauPrixVert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10.99 €" android:textColor="@color/green" android:textStyle="bold" /> <TextView android:id="@+id/ancienPrixRouge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="7dp" android:layout_marginRight="6dp" android:text="20.90 €" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/red" android:textSize="13sp" android:textStyle="bold" /> <TextView android:id="@+id/reductionPrix" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="4dp" android:text="(-34%)" android:textColor="@color/red" android:textStyle="bold" /> </LinearLayout> </RelativeLayout> <LinearLayout android:id="@+id/ligneTwo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="3dp" android:orientation="vertical" > <TextView android:id="@+id/titreEnGros" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_marginTop="3dp" android:text="Here is an example of a compleete title in a TextView Android" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <RelativeLayout android:id="@+id/ligneFour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:orientation="vertical" > <TextView android:id="@+id/dateAjout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="il y a 2 heures" /> <TextView android:id="@+id/siteWeb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:text="Amazon" /> </RelativeLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
If you have some ideas to center the block, please help me !
-
nibbana almost 5 yearsThe centered LinearLayout also needs layout_height="wrap_content"