济南达内IT培训
美国上市IT培训机构

17156168575

热门课程

Android中实现设置文字之间的距离

  • 时间:2016-07-30
  • 发布:济南达内
  • 来源:济南达内

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);

}

}

上一篇:web前段开发需要掌握什么技术
下一篇:Android应用的排名算法

马上预约七天免费体验课

姓名:

电话:

Java面试题(终)

Java面试题(十五)

Java面试题(十四)

Java面试题(十三)

选择城市和中心
贵州省

广西省

海南省

达内教育

有位老师想和您聊一聊