Reputation: 2324
I am trying to center a TextView inside a RelativeLayout, but for some reason the position of the TextView is not exactly in the center, it's in the center and a little down. This problem occurs when I add the second RelativeLayout, the one with no id.
Here are 2 ways I tried to solve my problem without success:
First:
<RelativeLayout
android:id="@+id/minus_one"
android:layout_width="45dp"
android:layout_height="match_parent"
android:background="@color/lightgrey">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ractangle_visual_feedback_selector"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:textStyle="bold"
android:text="-"/>
</RelativeLayout>
</RelativeLayout>
Second:
<RelativeLayout
android:id="@+id/minus_one"
android:layout_width="45dp"
android:layout_height="match_parent"
android:background="@color/lightgrey">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ractangle_visual_feedback_selector">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:textStyle="bold"
android:text="-"/>
</RelativeLayout>
</RelativeLayout>
Upvotes: 1
Views: 1499
Reputation: 21736
#. If your TextView
height-width is used as fixed
size or wrap_content
, then use attribute android:layout_centerInParent="true"
to TextView
to align it center
position of its parent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light">
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="22dp"
android:textStyle="bold"
android:text="-"
android:background="@android:color/white"/>
</RelativeLayout>
</RelativeLayout>
OUTPUT:
#. If your TextView
height-width is used as match_parent
, then use attribute android:gravity="center"
to TextView
to align it center
position of its parent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light">
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="22dp"
android:textStyle="bold"
android:text="-"
android:background="@android:color/white"/>
</RelativeLayout>
</RelativeLayout>
OUTPUT:
Hope this will help~
Upvotes: 3
Reputation: 5684
Try below code:
<?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:id="@+id/minus_one"
android:layout_width="45dp"
android:layout_height="match_parent"
android:background="@color/gray"
android:layout_gravity="center">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="22dp"
android:textAlignment="center"
android:gravity="center_vertical"
android:textStyle="bold"
android:text="-"/>
</RelativeLayout>
</RelativeLayout>
Upvotes: 1
Reputation: 302
try this android:layout_centerInParent="true"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dp"
android:textStyle="bold"
android:layout_centerInParent="true"
android:text="-"/>
</RelativeLayout>
Upvotes: 2
Reputation: 11245
Change root Relative Layout
android:layout_width to "match_parent"
. Remove android:gravity="center"
from parent Relative Layout
no needful. Now add the android:layout_centerInParent="true"
to your Text View
.
Try this xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/minus_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Testing"
android:textSize="22dp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
Upvotes: 5
Reputation: 5279
change the layout_width to match parent
android:layout_width="match_parent"
Upvotes: 2