TextView中如何让边框不随滚动条移动。
首先代码如下。
public class Show extends LinearLayout{
Context parentContext;
Activity activity;
private TextView tv;
private Button button;
public Show(Context context) {
super(context);
this.parentContext=context;
activity=(Activity) parentContext;
init();
}
private void init() {
this.setOrientation(VERTICAL);
// TODO Auto-generated method stub
TextView title=new TextView(parentContext);
title.setText("标题");
addView(title,new LayoutParams(-1,-2));
tv=new MyTextView(parentContext);
tv.setPadding(5, 5, 5, 5);
LayoutParams lp=new LayoutParams(-1, 200);
System.out.println(tv);
//android:scrollbars="vertical"
tv.setVerticalScrollBarEnabled(true);
String str="1. 选项1\n2. 选项2\n3. 选项3\n4. 选项4\n5. 选项5\n6. 选项6\n7. 选项7\n8. 选项8\n9. 选项9\n10. 选项10\n11. 选项11\n12. 选项12\n13. 选项13\n14. 选项14\n15. 选项15";
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
SpannableStringBuilder style=new SpannableStringBuilder(str);
Pattern p = Pattern.compile(".*?(\\d+\\. ).*?");
Matcher m = p.matcher(str);
int i=0;
while(m.find()){
String find=m.group(1);
i = str.indexOf(find,i);
System.out.println(str.substring(i,i+find.length()));
style.setSpan(new ForegroundColorSpan(Color.RED),i,i+find.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
button=new Button(parentContext);
button.setText("按钮");
addView(tv,lp);
addView(button,new LayoutParams(-1,-2));
}
}
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context) {
this(context, null);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
// 将边框设为蓝色
paint.setColor(android.graphics.Color.BLUE);
//设置边线的宽度
paint.setStrokeWidth(5);
// 画TextView的4个边
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);
}
}
private void init() {
layout.setOrientation(LinearLayout.VERTICAL);
// TODO Auto-generated method stub
TextView title = new TextView(this);
title.setText("标题");
layout.addView(title, new LayoutParams(-1, -2));
tv = new TextView(this);
tv.setPadding(5, 5, 5, 5);
System.out.println(tv);
// android:scrollbars="vertical"
// tv.setVerticalScrollBarEnabled(true);
String str = "1. 选项1\n2. 选项2\n3. 选项3\n4. 选项4\n5. 选项5\n6. 选项6\n7. 选项7\n8. 选项8\n9. 选项9\n10. 选项10\n11. 选项11\n12. 选项12\n13. 选项13\n14. 选项14\n15. 选项15";
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
SpannableStringBuilder style = new SpannableStringBuilder(str);
Pattern p = Pattern.compile(".*?(\\d+\\. ).*?");
Matcher m = p.matcher(str);
int i = 0;
while (m.find()) {
String find = m.group(1);
i = str.indexOf(find, i);
System.out.println(str.substring(i, i + find.length()));
style.setSpan(new ForegroundColorSpan(Color.RED), i,
i + find.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
button = new Button(this);
button.setText("按钮");
ScrollView sv = new MyScrollView(this);
LayoutParams lp = new LayoutParams(-1, 200);
sv.addView(tv);
layout.addView(sv, lp);
layout.addView(button, new LayoutParams(-1, -2));
}
public class MyScrollView extends ScrollView {
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyScrollView(Context context) {
this(context, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 画边框
Rect rec = canvas.getClipBounds();
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rec, paint);
}
}