Reprint please indicate source: http://blog.csdn.net/u011974987/article/details/50845269;
In Android, TextView is more compact when it is displayed in Chinese by default. Not very beautiful. In order to maintain a certain line spacing for each line, you can set the property Android:linespacingextra or Android:linespacingmultiplier.
But sometimes we need to have space between the TextView text, two words, we can in the XML file. It is accomplished by knocking the blanks. Suppose there is a lot of text or a variable text.
Do we still use the way to knock the space to achieve it? Oh no~!
How do you achieve line spacing and text spacing? (please look down ↓).
1, set the line spacing of TextView
To add a property to the TextView control:
android:lineSpacingExtra="13dp" //设置行间距 android:lineSpacingMultiplier="1.2" //设置行间距的倍数。
如”1.2”
2, set the text spacing of TextView
- First look at the following:
- The TextView code of your own definition is as follows:
PackageCom.woyou.spacingtextview;ImportAndroid.content.Context;Importandroid.text.Spannable;Importandroid.text.SpannableString;ImportAndroid.text.style.ScaleXSpan;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView;/** * Created by Xiho on 2016/3/7. * * Public class spacingtextview extends TextView{ Private floatletterspacing = Letterspacing.biggest;PrivateCharsequence Originaltext =""; Public Spacingtextview(Context context) {Super(context); } Public Spacingtextview(context context, AttributeSet attrs) {Super(context, attrs); Originaltext =Super. GetText (); Applyletterspacing (); This. Invalidate (); } Public Spacingtextview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); } Public float getletterspacing() {returnletterspacing; } Public void setletterspacing(floatletterspacing) { This. letterspacing = letterspacing; Applyletterspacing (); }@Override Public void SetText(charsequence text, Buffertype type) {originaltext = text; Applyletterspacing (); }@Override PublicCharsequenceGetText() {returnOriginaltext; }/** * kerning for whatever string (technically, a simple method for charsequence not used) is TextView */ Private void applyletterspacing() {if( This==NULL|| This. Originaltext = =NULL)return; StringBuilder Builder =NewStringBuilder (); for(inti =0; I < originaltext.length (); i++) {String c =""+ Originaltext.charat (i); Builder.append (C.tolowercase ());if(i+1< Originaltext.length ()) {Builder.append ("\U00A0"); }} spannablestring Finaltext =NewSpannablestring (Builder.tostring ());if(Builder.tostring (). Length () >1) { for(inti =1; I < builder.tostring (). Length (); i+=2) {Finaltext.setspan (NewScalexspan ((letterspacing+1)/Ten), I, i+1, spannable.span_exclusive_exclusive); } }Super. SetText (Finaltext, buffertype.spannable); } Public class letterspacing { Public Final Static floatNORMAL =0; Public Final Static floatNormalbig = (float)0.025; Public Final Static floatBIG = (float)0.05; Public Final Static floatBiggest = (float)0.2; }}
Private Spacingtextview Mspacingtextview;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Mspacingtextview = (Spacingtextview) Findviewbyid (R. ID. Space_text);Mspacingtextview. SetText(Getresources (). getString(R. String. Test));//Orany float. To reset to normal, use0 orLetterspacingtextview. Spacing. NORMALMspacingtextview. setletterspacing(Ten);}
- XML files such as the following:
<?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" Xmlns:tools="Http://schemas.android.com/tools" Android:layout_width="Match_parent" Android:layout_height="Match_parent" Android:paddingbottom="@dimen/activity_vertical_margin" Android:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" App:layout_behavior="@string/appbar_scrolling_view_behavior" Tools:context="Com.woyou.spacingtextview.MainActivity" Tools:showin="@layout/activity_main"> <com.woyou.spacingtextview.SpacingTextViewandroid:id="@+id/space_text" Android:layout_width="Wrap_content"android:linespacingextra="13DP"android: Layout_height="Wrap_content"android:text="@string/test" /> </relativelayout>
The above code is very convenient to use. Add to your project to see the effect.
TextView control to open Source: Spacingtextview
Suppose you want to achieve TextView text alignment; Open Source project: Aligntextview
Android self-defined textview for text spacing