栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Android中对齐文本

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Android中对齐文本

我使用以下代码来回答非常需要此主题的人们,并创建在每个显示中都支持的公式。

    public class TextJustify {final static String SYSTEM_newline = "n";final static float COMPLEXITY = 5.12f; // Reducing this will increase       // efficiency but will decrease       // effectivenessfinal static Paint p = new Paint();public static void run(final TextView tv, float origWidth, int paddingLeft, int paddingRight, int marginLeft, int marginRight) {    origWidth-= paddingRight+marginRight+paddingLeft+marginLeft;    String s = tv.getText().toString();    p.setTypeface(tv.getTypeface());    String[] splits = s.split(SYSTEM_newline);    float width = origWidth - 5;    for (int x = 0; x < splits.length; x++)        if (p.measureText(splits[x]) > width) { splits[x] = wrap(splits[x], width, p); String[] microSplits = splits[x].split(SYSTEM_newline); for (int y = 0; y < microSplits.length - 1; y++)     microSplits[y] = justify(removeLast(microSplits[y], " "),  width, p); StringBuilder smb_internal = new StringBuilder(); for (int z = 0; z < microSplits.length; z++)     smb_internal.append(microSplits[z]  + ((z + 1 < microSplits.length) ? SYSTEM_newline          : "")); splits[x] = smb_internal.toString();        }    final StringBuilder smb = new StringBuilder();    for (String cleaned : splits)        smb.append(cleaned + SYSTEM_newline);    tv.setGravity(Gravity.RIGHT);    tv.setText(smb);}private static String wrap(String s, float width, Paint p) {    String[] str = s.split("\s"); // regex    StringBuilder smb = new StringBuilder(); // save memory    smb.append(SYSTEM_newline);    for (int x = 0; x < str.length; x++) {        float length = p.measureText(str[x]);        String[] pieces = smb.toString().split(SYSTEM_newline);        try { if (p.measureText(pieces[pieces.length - 1]) + length > width)     smb.append(SYSTEM_newline);        } catch (Exception e) {        }        smb.append(str[x] + " ");    }    return smb.toString().replaceFirst(SYSTEM_newline, "");}private static String removeLast(String s, String g) {    if (s.contains(g)) {        int index = s.lastIndexOf(g);        int indexEnd = index + g.length();        if (index == 0) return s.substring(1);        else if (index == s.length() - 1) return s.substring(0, index);        else return s.substring(0, index) + s.substring(indexEnd);    }    return s;}private static String justifyOperation(String s, float width, Paint p) {    float holder = (float) (COMPLEXITY * Math.random());    while (s.contains(Float.toString(holder)))        holder = (float) (COMPLEXITY * Math.random());    String holder_string = Float.toString(holder);    float lessThan = width;    int timeOut = 100;    int current = 0;    while (p.measureText(s) < lessThan && current < timeOut) {        s = s.replaceFirst(" ([^" + holder_string + "])", " "     + holder_string + "$1");        lessThan = p.measureText(holder_string) + lessThan     - p.measureText(" ");        current++;    }    String cleaned = s.replaceAll(holder_string, " ");    return cleaned;}private static String justify(String s, float width, Paint p) {    while (p.measureText(s) < width) {        s = justifyOperation(s, width, p);    }    return s;}  }

为了调用此代码,您必须使用以下代码,我测试了波斯语语言,并且在每种显示和设备上都运行良好。

     public static final int FinallwidthDp  = 320 ;     public static final int widthJustify  = 223 ;     DisplayMetrics metrics = new DisplayMetrics();     getWindowManager().getDefaultDisplay().getMetrics(metrics);     int widthPixels = metrics.widthPixels;     float scaleFactor = metrics.density;     float widthDp = widthPixels / scaleFactor;     TextView tv = (TextView) findViewById(R.id.textView1);     ViewGroup.MarginLayoutParams lp1 = (ViewGroup.MarginLayoutParams) tv.getLayoutParams();     tv.setText(text);     TextJustify.run(tv,widthDp / FinallwidthDp * widthJustify , tv.getPaddingLeft(),tv.getPaddingRight() , lp1.leftMargin, lp1.rightMargin);

此算法已在各种设备上进行了测试,并且在正常活动(非对话框)和的

wrap-content
宽度上都可以正常
TextView
工作,并且可以处理所有的填充和边距。如果对您不利,您可以进行更改,
widthJustify
直到对您有利为止,希望这一点有用。对于新更新看到这



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号