Android defines line wrapping center Centertextview

Source: Internet
Author: User

When we develop the app, TextView must be the most used control, and the TextView of Android comes with a powerful feature. But there are some small places that can't meet our needs. A few days to say this function is also in the development of non-common. is when we display a string that exceeds the width of the screen. TextView will take the initiative to break the line, but the system default line-wrapping effect is the top, not the center of the art requirements.

At this time, we need to make some modifications to the system's TextView. The text can be centered after the line break has been made.

First look at the following:

This layout is very easy on iOS, and Android also defines a view.

Idea: When looking at the source code in the Android.text package. Discover several classes that have never been used. Contains: Layout,staticlayout,deynamiclayout and several other classes. Baidu learned about the general role of these categories:

                 these three layout, It is used to layout the Android Charsequence and its subclasses, and for it to pass in different alignment, according to different alignment to deal with. The code is very easy, just from writing TextView. Code such as the following:

Package Com.example.materialdesigndemo;import Android.content.context;import Android.graphics.canvas;import Android.graphics.paint;import Android.text.layout.alignment;import Android.text.staticlayout;import Android.text.textpaint;import Android.util.attributeset;import android.widget.textview;/************************ * @ FileName: Centertextview.java * @ file Rzq * @ Created: July 2, 2015 10:12:16 * @ File Description: NewLine Center display TextView * @ change history: July 2, 2015 create an initial version number **********************************************************/public Class Centertextview extends Textview{private staticlayout mystaticlayout;private textpaint tp;public CenterTextView ( Context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH); Initview ();} private void Initview () {tp = new Textpaint (paint.anti_alias_flag); Tp.settextsize (GetTextSize ()); Tp.setcolor ( Getcurrenttextcolor ()); mystaticlayout = new Staticlayout (GetText (), TP, GetWidth (), Alignment.align_center, 1.0f, 0.0f, false);} @Overrideprotected void OnDraw (canvas canvas) {mystaticlayout.draw (canvas);}}
Use:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <textview        android:id= "@+id/text"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:layout_centerhorizontal= "true"        android:layout_margintop= "30DP        " Android:text= "The most beautiful thing is not the rainy day. is with you to escape the eaves of rain, beautiful picture.

"/> <com.example.materialdesigndemo.centertextview android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_below= "@id/text" android:layout_centerhorizontal = "true" android:layout_margintop= "30DP" android:text= "the most beautiful is not the rainy day. is with you to escape the eaves of rain, beautiful picture. "/></relativelayout>

The code is very easy and basically only needs to rewrite the OnDraw () method. Let the Staticlayout instance go through the process again. The drawback is that after this treatment. Our Centertextview can only display text. It is not possible to display drawableleft and so on, if necessary, more complex processing is required in the OnDraw () method.

Demo

Android defines line wrapping center Centertextview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.