开发者

align text center with android

I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.

Here's my code :

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_heigh开发者_如何学运维t="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_centerHorizontal="true"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

I put paddingTop and Bottom because I need some space. PS: My code is bigger; it's in a RelativeLayout.


Set also android:gravity parameter in TextView to center.

For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.


use this way

txt.setGravity(Gravity.CENTER);


use this way in xml

   <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time is precious, so love now."
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#fff"
        />


You can use the following:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

 <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Your text"
        android:typeface="serif" />
</LinearLayout>

The layout needs to be relative for the "center" property to be used.


Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!

Also, you should avoid using dp for font size. Use sp instead.


Just add these 2 lines;

android:gravity="center_horizontal"
android:textAlignment="center"


add layout_gravity and gravity with center value on TextView

<TextView
    android:text="welcome text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    />


android:layout_gravity="center"

or

android:gravity="center"

both works for me.


Or check this out this will help align all the elements at once.

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>


If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:

chatMsg.setGravity(Gravity.RIGHT);

chatMsg is my textView. I wanted the text to be right aligned and this code worked well.


you can easily done this by adding these lines

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜