Android custom line feed center CenterTextView, androidtextview line feed

Source: Internet
Author: User

Android custom line feed center CenterTextView, androidtextview line feed

When we develop an app, TextView must be the most used control. The TextView function provided by android is also very powerful, but it still cannot meet our needs in some small places, this function is also very common during development, that is, when we display a String that exceeds the screen width, TextView will automatically wrap, but the default line feed effect is the top, it is not the center required by the artist. At this time, we need to make some modifications to the TextView of the system, so that the text can be centered after the line break.

Let's take a look at it first:

This layout is easily implemented on IOS, and android also customizes a View.

Idea: Watch android. when the source code in the text package is used, several useless classes are found, including Layout, StaticLayout, DeynamicLayout, and so on. Baidu later learns about the functions of these classes:

The three Layout clauses are used to Layout the CharSequence of android and its sub-classes. Different Alignment clauses are input for them, which are processed according to different Alignment clauses. The code is simple. You only need to write TextView. The Code is as follows:

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; /*************************************** * ******************** @ File Name: centerTextView. java * @ file Author: rzq * @ Creation Time: 10:12:16, January 1, July 2, 2015 * @ file Description: center line feed display TextView * @ modification history: create the initial version ************************************** on April 9, July 2, 2015 ************************************ * *******************/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 );}}
Usage:
<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, it is with you The beautiful pictures escape the rain. "/> <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 thing is not a rainy day, it is a beautiful picture that hides the rain with you. "/> </RelativeLayout>

The code is very simple. Basically, you only need to rewrite the onDraw () method and let the StaticLayout instance reprocess it. After processing, the disadvantage is that our CenterTextView can only display text, but cannot display drawableLeft. If necessary, we need to perform more complex processing in the onDraw () method.

Demo

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.