Android: align text to the left and center text in parent view

Hello, I ran into a little problem.

I use GRAVITY LEFT to make text for the left side of the view, but I want to focus inside the text box, but also align to the left

Here is what I have now:

___________________________________ _ _ _ _ _ _| aaaaaaaaaaaaa |_ _ _ _ _ _ _ _ _ _ _ _| aaaaaaaa |_ _ _ _ _ _ _ _ _ _ _ _| aaaaaaaaaaaaaa |_ _ _ _ _ _ ----------------------------------- 

I want:

  ___________________________________ _ _ _ _ _ _| aaaaaaaaaaa |_ _ _ _ _ _ _ _ _ _ _ _| aaaaaaaa |_ _ _ _ _ _ _ _ _ _ _ _| aaaaaaaaaaaaaa |_ _ _ _ _ _ ----------------------------------- 

Where:

_ _ _ _ = outside textview

| = edge of TextView

I tried android: gravity = "left | center", but this does not work, any idea?

thanks

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/image_button_wrapper" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_centerVertical="true"> <ImageView android:id="@+id/left_button_image" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="@dimen/spacing_normal"/> <Textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/left_button_image" android:gravity = "center|left|left" android:clickable="false" android:focusable="false" app:font_name="Roboto-Light.ttf"/> 

What am I (text aligned left but not centered) enter image description here

What I want (the text is aligned left and center) (I add the addition manually to get the render, but it is dirty and will not adapt to the whole text): enter image description here

+8
android text textview gravity
source share
5 answers

Just specify the Textview parent as:

 android:gravity = "center" 

Just give Textview like:

 android:gravity = "center_vertical|left|start" android:layout_margin="20dp" 
+4
source share

You will get what you want if you change the width of the text field to match_parent or set a fixed width:

 <TextView android:layout_width="100dp" //android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hi! This is amazing!!" android:gravity="center_horizontal"/> 
+2
source share

In text mode, change this:

  android:gravity = "center|left|left" 

to

  android:gravity = "center" 
+1
source share

The problem is your text width, which simply wraps itself around the text and sets its gravity to center it only in the wrap area.

Change the width to fit - parent and gravity - to the center. It can help you.

 android:layout_width="match_parent" android:gravity = "center" 

Alternative method: because the boundary lines are centered, which are separated from the text view. You simply add the following line to the text view.

  android:layout_centerHorizontal="true" 
+1
source share

change RelativeLayout to LinearLayout, create a center of gravity and layout_gravity.

0
source share

All Articles