1. /TestScrollView/res/layout/main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/scrollview" android:layout_width="fill_parent"
4 android:layout_height="wrap_content" android:scrollbars="none">
5 <LinearLayout
6 android:id="@+id/linearlayout"
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:orientation="vertical"
10 >
11 <TextView
12 android:layout_width="fill_parent"
13 android:layout_height="wrap_content"
14 android:text="@string/hello"
15 />
16 <Button
17 android:id="@+id/button01"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:text="Button01"
21 />
22 </LinearLayout>
23 </ScrollView>
2. TestScrollViewActivity:
1 import android.app.Activity;
2 import android.os.Bundle;
3 import android.os.Handler;
4 import android.view.KeyEvent;
5 import android.view.View;
6 import android.view.View.OnClickListener;
7 import android.view.View.OnKeyListener;
8 import android.widget.Button;
9 import android.widget.LinearLayout;
10 import android.widget.ScrollView;
11 import android.widget.TextView;
12
13 public class TestScrollViewActivity extends Activity
14 {
15 private Button button;
16 private LinearLayout mLinearLayout;
17 private ScrollView mScrollView;
18 private final Handler mHandler = new Handler();
19 @Override
20 public void onCreate(Bundle savedInstanceState)
21 {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.main);
24 mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
25 mScrollView = (ScrollView) findViewById(R.id.scrollview);
26 button = (Button) findViewById(R.id.button01);
27 button.setOnClickListener(listener);
28 /*
29 * 改变默认焦点切换
30 */
31 button.setOnKeyListener(keyListener);
32 }
33
34 private OnClickListener listener = new OnClickListener()
35 {
36 private int mIndext = 1;
37
38 @Override
39 public void onClick(View arg0)
40 {
41 TextView textView = new TextView(TestScrollViewActivity.this);
42 textView.setText("Text View " + mIndext);
43 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
44 LinearLayout.LayoutParams.WRAP_CONTENT);
45 // 增加一个TextView到线性布局中
46 mLinearLayout.addView(textView, params);
47 Button buttonView = new Button(TestScrollViewActivity.this);
48 buttonView.setText("Button " + mIndext++);
49 // 增加一个Button到线性布局
50 mLinearLayout.addView(buttonView, params);
51 // 改变默认焦点切换
52 buttonView.setOnKeyListener(mNewButtonKeyListener);
53 // 投递一个消息进行滚动
54 mHandler.post(mScrollToButton);
55 }
56 };
57
58 private Runnable mScrollToButton = new Runnable()
59 {
60 @Override
61 public void run()
62 {
63 int off = mLinearLayout.getMeasuredHeight() - mScrollView.getHeight();
64 if (off > 0)
65 {
66 mScrollView.scrollTo(0, off);
67 }
68 }
69 };
70
71 // 事件监听
72 private OnKeyListener mNewButtonKeyListener = new OnKeyListener()
73 {
74 @Override
75 public boolean onKey(View v, int keyCode, KeyEvent event)
76 {
77 if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_DOWN
78 && v == mLinearLayout.getChildAt(mLinearLayout.getChildCount() - 1))
79 {
80 findViewById(R.id.button01).requestFocus();
81 return true;
82 }
83 return false;
84 }
85 };
86
87 private OnKeyListener keyListener = new OnKeyListener()
88 {
89 @Override
90 public boolean onKey(View v, int keycode, KeyEvent event)
91 {
92 View viewToFoucus = null;
93 if (event.getAction() == KeyEvent.ACTION_DOWN)
94 {
95 int iCount = mLinearLayout.getChildCount();
96 switch (keycode)
97 {
98 case KeyEvent.KEYCODE_DPAD_UP:
99 if(iCount>0){
100 viewToFoucus = mLinearLayout.getChildAt(iCount-1);
101 }
102 break;
103 case KeyEvent.KEYCODE_DPAD_DOWN:
104 if(iCount<mLinearLayout.getWeightSum()){
105 viewToFoucus = mLinearLayout.getChildAt(iCount+1);
106 }
107 break;
108 default:
109 break;
110 }
111 if(viewToFoucus!=null){
112 viewToFoucus.requestFocus();
113 return true;
114 }else{
115 return false;
116 }
117 }
118 return false;
119 }
120 };
121 }
3. 效果图: