首先代码如下。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);
    }
}效果如下:
问:如何不让边框随着移动?

解决方案 »

  1.   


    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
      

  2.   

    你这样的方式其实也不是我想要的方法,这种方法我也试过,但是这种方法是把整个TextView包裹起来,
    而且想要的效果是显示框就固定在那里不动,显示框里面就是那些可以显示的部分。
    这样的话就没有上下边框了,
      

  3.   

    那也简单啊,设置scrollview的边框,自定义scrollview,而不是textview
    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);
    }
    }