美文网首页
Android TextView 古诗风格排版

Android TextView 古诗风格排版

作者: 奈蜇 | 来源:发表于2020-08-10 15:18 被阅读0次

直入主题,你有一段古诗文字,然后你要显示出来。
显而易见你可以这样显示
↓↓↓↓↓↓↓

       南陵别儿童入京
        作者:李白
白酒新熟山中归,黄鸡啄黍秋正肥。
呼童烹鸡酌白酒,儿女嬉笑牵人衣。
高歌取醉欲自慰,起舞落日争光辉。
游说万乘苦不早,著鞭跨马涉远道。
会稽愚妇轻买臣,余亦辞家西入秦。
仰天大笑出门去,我辈岂是蓬蒿人。

直接丢到textview中看看效果


横版

会心一笑的点了点头 easy 😁😁😁

美工一看不高兴了 说这个太像课本了 不好看 我要竖着来!!!

emmmm 你事真多!!!😒😒😒
行 你厉害 听你的......

先看效果
↓↓↓↓↓↓↓


竖版

还行吧~~~

好了 看看怎么做的

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * author : 奈蜇
 * e-mail : 1104332403@qq.com
 * date   : 2020/8/10
 * desc   : 古诗格式的TextView
 * version: 1.0
 */
public class VerticalTextView extends TextView {

    //原文
    private String oldText;
    //分割符
    private String separator;
    //是否从右边开始
    private boolean isRight = false;

    public VerticalTextView(Context context) {
        this(context, null);
    }

    public VerticalTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerticalTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        //设置每个字符的宽度一样
        setTypeface(Typeface.MONOSPACE);
    }

    public String getSeparator() {
        return separator;
    }

    public void setSeparator(String separator) {
        this.separator = separator;
    }

    public boolean isRight() {
        return isRight;
    }

    public void setRight(boolean right) {
        isRight = right;
    }

    private String horizontalText2VerticalText(String horizontalText, String separator, boolean right) {
        //转换
        String[] horizontalTextArr = horizontalText.split(separator);
        int lineNumber = horizontalTextArr.length;
        int aLineTextMaxLength = 0;
        char[][] chars = new char[lineNumber][];
        for (int i = 0; i < lineNumber; i++) {
            //读取单行内容
            String aLineText = horizontalTextArr[i];
            //单行的长度
            int aLineTextLength = aLineText.length();
            //对比长度
            if (aLineTextLength > aLineTextMaxLength) {
                aLineTextMaxLength = aLineTextLength;
            }
            //转换单行内容成字符数组
            chars[i] = aLineText.toCharArray();
        }
        StringBuilder verticalStringBuilder = new StringBuilder();
        for (int i = 0; i < aLineTextMaxLength; i++) {
            if (right) {
                for (int j = lineNumber - 1; j >= 0; j--) {
                    //短句字符已"用完"则以空格代替
                    char c = i < chars[j].length ? chars[j][i] : ' ';
                    verticalStringBuilder.append(c);
                    //两列文字之间加空格,不需要的话请注释掉下面一行//★
                    if (0 < j && j <= lineNumber - 1) verticalStringBuilder.append(' ');
                }
            } else {
                for (int j = 0; j < lineNumber; j++) {
                    //短句字符已"用完"则以空格代替
                    char c = i < chars[j].length ? chars[j][i] : ' ';
                    verticalStringBuilder.append(c);
                    //两列文字之间加空格,不需要的话请注释掉下面一行//★
                    if (j < lineNumber - 1) verticalStringBuilder.append(' ');
                }
            }
            verticalStringBuilder.append("\n");
        }
        //返回前转化成全半角
        return toSBC(verticalStringBuilder.toString());
    }

    public String toSBC(final String s) {
        if (s == null || s.length() == 0) return "";
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == ' ') {
                chars[i] = (char) 12288;
            } else if (33 <= chars[i] && chars[i] <= 126) {
                chars[i] = (char) (chars[i] + 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (separator == null) separator = "\n";
        oldText = text.toString();
        super.setText(horizontalText2VerticalText(text.toString(), separator, isRight), type);
    }

    @Override
    public CharSequence getText() {
        return oldText;
    }

}

思路

这里是重点!!!!!!!!!

"ABC\nABC\nABC" 这个字符串在textview是这样显示的
ABC
ABC
ABC
横排哦
如果我把上方的字符串变化一下 变成 "AAA\nBBB\nCCC"
它是不是就这样显示了哦
AAA
BBB
CCC

完美 ,思路一下就清晰了 你应该懂了吧 你细品。
还有就是一些细节问题 你看看我上面的代码吧...
好吧 其实思路我也是抄的

本文参考:https://blog.csdn.net/midong2000/article/details/25662105?utm_source=blogxgwz1

相关文章

网友评论

      本文标题:Android TextView 古诗风格排版

      本文链接:https://www.haomeiwen.com/subject/lohjdktx.html