Android自定义TextView实现设置文字之间的距离
程序员文章站
2022-03-01 12:40:32
...
Android系统中TextView默认显示中文时会比较紧凑、不是很美观、但是有时候我们需要在TextView的文本之间有间距、就需要自定义TextView 来实现了、可不是敲空格能实现的、下面这个Demo就是一个自定义的View实现了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); } }
推荐阅读
-
Android TextView实现带链接文字事件监听的三种常用方式示例
-
android TextView设置中文字体加粗实现方法
-
Android实现TextView中文字链接的4种方式介绍及代码
-
解析在Android中为TextView增加自定义HTML标签的实现方法
-
Android 开发中根据搜索内容实现TextView中的文字部分加粗
-
android显示TextView文字的倒影效果实现代码
-
Android使用TypeFace设置TextView的文字字体
-
Android 开发中根据搜索内容实现TextView中的文字部分加粗
-
Android开发之自定义EditText实现保留两位小数(附EditText光标宽度、颜色、高度的设置)
-
Android 实现带头部文字输入框的自定义控件