【问题标题】:Provide kerning/Tracking for text in android为android中的文本提供字距调整/跟踪
【发布时间】:2011-08-02 13:31:18
【问题描述】:

我正在尝试增加文本中两个单词之间的间距。为此,我遇到了很多网站,但我找到了下面的网站,其中包含我需要的确切信息。

http://developer.android.com/reference/java/awt/font/TextAttribute.html

例如我想使用下面给定的常量

public static final TextAttribute TRACKING 

我的文字。我该怎么做?

在这里,他们列出了我可以与文本一起使用的所有 textAttributes(constants)。但我不知道如何将它用于我的文本。谁可以帮我这个事。

【问题讨论】:

    标签: android android-textattributes


    【解决方案1】:

    我写了这个函数:

    输入

    • 线性布局二:

      将成为您的“TexView”的布局(确保其方向是垂直的)

    • 字符串文本:

      正文

    • 上下文 mContext

      上下文

    做什么

    我评论了你必须编辑的部分


    /*
    *  Copyright 2011 Sherif
    */
    
    private void populateText(LinearLayout ll, String text , Context mContext) { 
        String [] textArray = text.split(" ");
        Display display = getWindowManager().getDefaultDisplay();
        ll.removeAllViews();
        int maxWidth = display.getWidth() - 20;
    
        LinearLayout.LayoutParams params; // to be used over and over
        LinearLayout newLL = new LinearLayout(mContext);
        newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        newLL.setGravity(Gravity.LEFT);
        newLL.setOrientation(LinearLayout.HORIZONTAL);
    
        int widthSoFar = 0;
    
        for (int i = 0 ; i < textArray.length ; i++ ){
            LL = new LinearLayout(mContext);
            LL.setOrientation(LinearLayout.HORIZONTAL);
            LL.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
            LL.setLayoutParams(new ListView.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
            TV = new TextView(mContext);
            TV.setText(textArray[i]);
            //TV.setTextSize(size);  <<<< SET TEXT SIZE
            TV.measure(0, 0);
            params = new LinearLayout.LayoutParams(TV.getMeasuredWidth(),
                    LayoutParams.WRAP_CONTENT);
            //params.setMargins(5, 0, 5, 0);  // YOU CAN USE THIS
            LL.addView(TV, params);
            LL.measure(0, 0);
            widthSoFar += TV.getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS
            if (widthSoFar >= maxWidth) {
                ll.addView(newLL);
    
                newLL = new LinearLayout(mContext);
                newLL.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                newLL.setOrientation(LinearLayout.HORIZONTAL);
                newLL.setGravity(Gravity.LEFT);
                params = new LinearLayout.LayoutParams(LL
                        .getMeasuredWidth(), LL.getMeasuredHeight());
                newLL.addView(LL, params);
                widthSoFar = LL.getMeasuredWidth();
            } else {
                newLL.addView(LL);
            }
        }
        ll.addView(newLL);
    }
    

    注意

    我还没有测试过它...我用它来处理更复杂的事情并且它正在工作

    【讨论】:

    • 但这是我唯一可以利用的方法吗?
    • 我会看看你是否可以使用 TRACKING... 但是你确定 TRACKING 负责间距吗?
    • 是的。我已经看到一些图像重新调整字距和跟踪。我敢肯定它
    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2023-03-30
    • 2012-12-08
    • 2016-10-16
    • 1970-01-01
    相关资源
    最近更新 更多