如何在Android textview中设置字体宽度?

问题描述 投票:13回答:3

我正在开发一个应用程序,显示对字体宽度非常敏感的ascii arts等信息。我一直在使用等宽字体,但由于信息中存在宽字符(例如中文和日文字符)而不能很好地工作,而textview不会使宽字符与常规字符完全相同。因此,我试图看看是否可以改变textview中字体的宽度,或者是否有更好的方法来解决这个问题?在我的应用程序中安装另一个等宽字体是个好主意吗?任何输入都非常感谢。谢谢。

凯文

android fonts width textview
3个回答
32
投票

你可以试试

textView.setTextScaleX(1.5f);

textView.setTextSize(20);

textView.setTypeface(Typeface.MONOSPACE);    //all characters the same width

使用这三种方法,我希望您可以将字体设置为理想的外观。


3
投票

对于那些想要使用xml设置monospace的人,请尝试添加'android:typeface =“monospace”'

        <TextView
            android:id="@+id/tv_output"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"  
            android:typeface="monospace" />

在添加monospace之前

enter image description here

添加monospace enter image description here之后


2
投票

这个问题太旧了,但我遇到了类似的问题,最终我找到了一个很好的解决方案。

我有字体,没有等宽变体。我需要在TextView中的几行中显示一些十六进制值,但我不想使用任何其他字体。

Android documentation说:

跨度是强大的标记对象,可用于在字符或段落级别设置文本样式。通过将跨距附加到文本对象,您可以通过多种方式更改文本,包括添加颜色,使文本可单击,缩放文本大小以及以自定义方式绘制文本。 Spans还可以更改TextPaint属性,在Canvas上绘制,甚至更改文本布局。

所以,我创建了自定义的MonospaceSpan实现,它源自ReplacementSpan。此跨度检测给定文本的最宽字符,并绘制具有相同宽度的其他字符。

结果如下:

GitHub

mono spaces盘.Java

    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.text.style.ReplacementSpan;

    public class MonospaceSpan extends ReplacementSpan {
        private boolean ignoreFullText;

        public void setIgnoreFullText(boolean ignoreFullText) {
            this.ignoreFullText = ignoreFullText;
        }

        private int getMaxCharWidth(@NonNull Paint paint, @NonNull CharSequence text, int start, int end, float[] widths) {
            if (widths == null) {
                widths = new float[end - start];
            }

            paint.getTextWidths(text, start, end, widths);

            float max = 0;

            for (float w : widths) {
                if (max < w) {
                    max = w;
                }
            }

            return Math.round(max);
        }

        @Override
        public int getSize(@NonNull Paint paint, @NonNull CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
            if (fm != null) {
                paint.getFontMetricsInt(fm);
            }

            int count = end - start;

            if (text.charAt(start) == '\n') {
                count -= 1;
            }

            if (text.charAt(end - 1) == '\n') {
                count -= 1;
            }

            if (count < 0) {
                count = 0;
            }

            if (ignoreFullText) {
                return getMaxCharWidth(paint, text, start, end, null) * count;
            } else {
                return getMaxCharWidth(paint, text, 0, text.length(), null) * count;
            }
        }

        @Override
        public void draw(@NonNull Canvas canvas, @NonNull CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
            float[] widths = new float[end - start];
            int max = getMaxCharWidth(paint, text, start, end, widths);

            if (!ignoreFullText) {
                max = getMaxCharWidth(paint, text, 0, text.length(), null);
            }

            for (int i = 0, n = end - start; i < n; ++i) {
                float p = (max - widths[i]) / 2;
                canvas.drawText(text, start + i, start + i + 1, x + max * i + p, y, paint);
            }
        }
    }

用法示例:

main activity.Java

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.SpannableString;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            String text = "Lorem ipsum\ndolor sit amet\n0123456789";
            SpannableString textMono = new SpannableString(text);
            textMono.setSpan(new MonospaceSpan(), 0, textMono.length(), 0);

            TextView textView1 = findViewById(android.R.id.text1);
            TextView textView2 = findViewById(android.R.id.text2);

            textView1.setText(text);
            textView2.setText(textMono);
        }
    }

RES /布局/ activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="#fa0"
            android:fontFamily="@font/fredoka_one" />

        <TextView
            android:id="@android:id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="#0af"
            android:fontFamily="@font/fredoka_one" />
    </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.