欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android自定义TextView实现设置文字之间的距离

程序员文章站 2022-03-01 12:40:32
...

Android系统中TextView默认显示中文时会比较紧凑、不是很美观、但是有时候我们需要在TextView的文本之间有间距、就需要自定义TextView 来实现了、可不是敲空格能实现的、下面这个Demo就是一个自定义的View实现了TextView的间距问题、效果图片如下

Android自定义TextView实现设置文字之间的距离


SpacingTextView代码

public class SpacingTextView extends TextView{
    private float letterSpacing = LetterSpacing.BIGGEST;
    private CharSequence originalText = "";


    public SpacingTextView(Context context) {
        super(context);
    }

    public SpacingTextView(Context context, AttributeSet attrs){
        super(context, attrs);
        originalText = super.getText();
        applyLetterSpacing();
        this.invalidate();
    }

    public SpacingTextView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }

    public float getLetterSpacing() {
        return letterSpacing;
    }

    public void setLetterSpacing(float letterSpacing) {
        this.letterSpacing = letterSpacing;
        applyLetterSpacing();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        applyLetterSpacing();
    }

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

    /**
     * 字距为任何字符串(技术上,一个简单的方法为CharSequence不使用)的TextView
     */
    private void applyLetterSpacing() {
        if (this == null || this.originalText == null) return;
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < originalText.length(); i  ) {
            String c = ""  originalText.charAt(i);
            builder.append(c.toLowerCase());
            if(i 1 < originalText.length()) {
                builder.append("u00A0");
            }
        }
        SpannableString finalText = new SpannableString(builder.toString());
        if(builder.toString().length() > 1) {
            for(int i = 1; i < builder.toString().length(); i =2) {
                finalText.setSpan(new ScaleXSpan((letterSpacing 1)/10), i, i 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        super.setText(finalText, BufferType.SPANNABLE);
    }

    public class LetterSpacing {
        public final static float NORMAL = 0;
        public final static float NORMALBIG = (float)0.025;
        public final static float BIG = (float)0.05;
        public final static float BIGGEST = (float)0.2;
    }
}


调用方式

public class MainActivity extends AppCompatActivity {
    private SpacingTextView mSpacingTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSpacingTextView = (SpacingTextView) findViewById(R.id.space_text);
        mSpacingTextView.setText(getResources().getString(R.string.test));
        //Or any float. To reset to normal, use 0 or LetterSpacingTextView.Spacing.NORMAL
        mSpacingTextView.setLetterSpacing(10);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}