Android自定义竖排自定义竖排TextView实现实例实现实例
Android自定义竖排自定义竖排TextView实现实例实现实例
前言:前言:
之前做联系人模块的时候遇到一个左侧索引控件,里面的字符都是竖直方向上排列的。当时这个控件是用一个图片代替的。现
在想来如果索引的字符变更了,那么就得重新更换图片了,感觉很麻烦。今天通过一个自定义TextView实现类似的功能。先
上效果图:
汉字和英文字符都可以竖直排列。结合联系人界面,可以将左侧的索引改成联系人的姓氏。
上代码:
测试用的Activity。
public class MainActivity extends Activity implements OnTouchListener {
private VerticalTextView mVerticalTextView;
private TextView mTextView;
private int mTextCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mVerticalTextView = (VerticalTextView) findViewById(R.id.vertical_tv);
mTextView = (TextView) findViewById(R.id.content_tx);
mTextCount = mVerticalTextView.getText().length();
mVerticalTextView.setOnTouchListener(this);
mTextView.setBackgroundColor(Color.LTGRAY);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float verticalTextViewHeight = mVerticalTextView.getHeight();
float y = event.getY();
int sectionPosition = (int) Math.ceil((y / verticalTextViewHeight)