Android阅读器文字分散对齐及TextView中的字分散对齐,首尾缩进

电子书阅读器文字分散对齐等。

> 引用:https://github.com/ufo22940268/Android-justifiedtextview

   https://github.com/ufo22940268/android-justifiedtextview/blob/master/justifytext-library/library_justifytext/src/main/java/me/biubiubiu/justifytext/library/JustifyTextView.java ,这个类是比较正常的
     默认Textview在一行快要结束的时候,如果在符号后面,有一个长的字符串,这时候,TextView就会自动换行,导致排版会参差不齐,而自动换行导致混乱的原因了是半角字符与全角字符占位不同,一般情况下,我们输入的数字、字母以及英文标点都是半角,中文是全角,因此占位的位置大家都不同。在中英文混输的时候,导致很多文字的排版都是参差不齐的。
     那么要想要解决此问题就在解决它们的占位问题,网上有种思路是把半角符号的数字和英文全部转化为全角,这样一个字符就和中文字符对齐了。但是这样的话,就会使英文字母和英文字母之间分的很开,导致十分的难看。下面我们就开始来解决排版的问题。
    我们先了解下StaticLayout(This is used by widgets to control text layout. ),这个东西,TextView中就是使用StaticLayout来进行文字的排版处理。而我们这边需要让它来预处理下排版的分布,然后根据其提供的getDesiredWidth(CharSequence source, TextPaint paint)方法来判断一行文字本来需要的宽度值,再固定一行的宽度值,利用
固定的宽度 - 文本宽度 / 文本字数 = 每个文字的间距
这个方法来计算出排版对齐时,每个文字之间的距离。

【1】 在TextView中用android:text="测试     文字"的时候,可以正常显示正确的空格数。但是如果使用资源文件android:text="@string/txt"的时候,不管我资源文件里面<string name="txt">测试         文字</string>
中间使用多少个空格,或者TAB。在模拟器上运行的时候,N个空格都只显示一个。
    解决办法:使用全角空格。
这个用途估计只有在设计登陆框的时候能用吧,比如下面这样。
用户名:
密   码:


【2】 http://stackoverflow.com/questions/1587056/android-string-concatenate-how-to-keep-the-spaces-at-the-end-and-or-beginnin
1.Even if you use string formatting sometimes you still need whitespaces at the beginning or the end. On these cases neither escaping with \ nor xml:space attribute helpes. You must use xml encoding&#160;for whitespaces.
eg:&#160;表示全角空格,
    <string name="aaa">你好&#160;&#160;&#160;&#160;&#160;&#160;啊</string>

2.I found a solution on this issue report : http://code.google.com/p/android-apktool/issues/detail?id=14
  This is the same idea that Duessi suggests. Insert \u0020 directly in the XML for a blank you would like to preserve.
  Example :
<string name="your_id">Score :\u0020</string>
The replacement is done at build time, therefore it will not affect the performance of your game

【3】加“\t”可能有对齐效果
----------------------------------------------------------------------------------------------
Android阅读器如何实现文字两端对齐,达到类似ireader一样的效果??
【1】在android中的webview中,可以对文本内容进行对齐,具体方法如下:
public class MainActivity extends Activity {
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    String htmlText = " %s ";  
    String myData = "Hello World! This tutorial is to show demo of displaying text with justify alignment in WebView.";  

    WebView webView = (WebView) findViewById(R.id.webView1);  
    webView.loadData(String.format(htmlText, myData), "text/html", "utf-8");  
}  
}
【2】方案二:使用textView改造:首先设置TextView的显示字体大小和文本内容,这里设置字体大小根据屏幕尺寸调整。然后调用自定义的类Textustification中的justify方法来实现TextView的分散对齐,两个参数分别是TextView控件以及控件的宽度。
MainActivity中:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
width = dm.widthPixels;
//根据屏幕调整文字大小
mArticleTextView.setLineSpacing(0f, 1.5f);
mArticleTextView.setTextSize(8*(float)width/320f);
//设置TextView
mArticleTextView.setText("TextView需要显示的文本内容");
TextJustification.justify(mArticleTextView,mArticleTextView.getWidth());


 >自定义的类TextJustification内容如下:

import java.util.ArrayList;
import android.graphics.Paint;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class TextJustification {
public static void justify(TextView textView, float contentWidth) {
    String text=textView.getText().toString();
    String tempText;
    String resultText = "";
    Paint paint=textView.getPaint();

    ArrayList<String> paraList = new ArrayList<String>();       
    paraList = paraBreak(text);       
    for(int i = 0; i<paraList.size(); i++) {           
        ArrayList<String> lineList=lineBreak(paraList.get(i).trim(),paint,contentWidth);           
        tempText = TextUtils.join(" ", lineList).replaceFirst("\\s*", "");           
        resultText += tempText.replaceFirst("\\s*", "") + "\n";       
    }               

    textView.setText(resultText);
}
//分开每个段落
public static ArrayList<String> paraBreak(String text, TextView textview) {
    ArrayList<String> paraList = new ArrayList<String>();
    String[] paraArray = text.split("\\n+");
       for(String para:paraArray) {
           paraList.add(para);
    }
    return paraList;
}

//分开每一行,使每一行填入最多的单词数
private static ArrayList<String> lineBreak(String text, Paint paint, float contentWidth){
    String [] wordArray=text.split("\\s");
    ArrayList<String> lineList = new ArrayList<String>();
    String myText="";

    for(String word:wordArray){
        if(paint.measureText(myText+" "+word)<=contentWidth)
            myText=myText+" "+word;
        else{
            int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" "));
            lineList.add(justifyLine(myText,totalSpacesToInsert));
            myText=word;
        }
    }
    lineList.add(myText);
    return lineList;
}
//已填入最多单词数的一行,插入对应的空格数直到该行满
private static String justifyLine(String text,int totalSpacesToInsert){
    String[] wordArray=text.split("\\s");
    String toAppend=" ";


    while((totalSpacesToInsert)>=(wordArray.length-1)){
        toAppend=toAppend+" ";
        totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1);
    }
    int i=0;
    String justifiedText="";
    for(String word:wordArray){
        if(i<totalSpacesToInsert)
            justifiedText=justifiedText+word+" "+toAppend;
        else               
            justifiedText=justifiedText+word+toAppend;
        i++;
    }

    return justifiedText;
}
}
  > 这个类完成了TextView内部文字的排版工作,主要分3个步骤:
1、将一篇文章按段落分成若干段(如果只有一段可以略去该步骤);
2、将每一段的文字拆分成各个单词,然后根据控件长度确定每一行最多可以填入的单词数,并且算出排满该行还需要填入几个空格。
3、填入空格。

------------------------------------------------------------------------------

> TextView头部和尾部缩进,分散对齐
解决Android中TextView首行缩进的问题- http://blog.csdn.net/zjy_hll/article/details/45077855
TextView改变部分字体的大小和颜色及首行缩进- http://blog.csdn.net/luohai859/article/details/46755617
webView加载页面也是一种方案

首行缩进:
方式一:(推荐)setText("\u3000\u3000"+xxxxx);
方式二:这种方式不同分辨率会有问题 setText(""+xxxxx);
半角:\u0020;全角:\u3000

android TextView 分散对齐(两端对齐) http://www.tuicool.com/articles/6b6nUbY
AlignTextView- https://github.com/androiddevelop/AlignTextView http://www.cnblogs.com/lcyty/p/3265335.html

两端分散对齐的Textview- http://blog.csdn.net/razor1991/article/details/51459678

你可能感兴趣的:(Android阅读器文字分散对齐及TextView中的字分散对齐,首尾缩进)