首先代码如下。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));
}
}
MyTextView类:
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);
}
}效果如下:
问:如何不让边框随着移动?
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));
}
}
MyTextView类:
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);
}
}效果如下:
问:如何不让边框随着移动?
解决方案 »
- 一个ListItem的布局问题,为何信息内容足够长的时候,会直接占据整个屏幕宽度?
- android 如何模拟一条短信
- [Android.mk]build\core\product.mk下的_product_stash_var_list作用
- 求助,Widget 显示不了
- 原生froyo,怎么定制关机动画!
- 安卓程序run后,AVD运行,但是程序并没有运行到AVD上
- 安卓错误求教大家
- 网易云音乐我的消息页面顶部tab导航实现
- android listview 居中高亮
- viewpager+fragment实现切换无法打开,闪退
- sqlite如何创建和打数据库在指定的路径
- 安卓开发中修改了java文件的位置,如何让其重新识别?
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 MyTextView(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 ScrollView(this);
LayoutParams lp=new LayoutParams(-1, 200);
sv.addView(tv);
layout.addView(sv,lp);
layout.addView(button,new LayoutParams(-1,-2));
}你应该将textview放到scrollview里面,将高度定义为scrollview的高度,而不是给textview设置scrollbar
而且想要的效果是显示框就固定在那里不动,显示框里面就是那些可以显示的部分。
这样的话就没有上下边框了,
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);
}
}