在加入ScrollView會順帶加入一個LinearLayout,這個LinearLayout有分垂直和水平的樣式
沒有去設定LinearLayout的樣式時,初始的樣式應該是水平,所以要記得去加入
android:orientation="vertical"
那為什麼要加入呢?下面這章圖是還沒設定成垂直樣式的畫面
還沒加入垂直樣式的手機畫面
加入後就可以看到如下圖的畫面
string.xml
<resources> <string name="app_name">ScrollView</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_scroll_view">ScrollView</string> <string name="one">One </string> <string name="two">Two</string> <string name="three">Three</string> <string name="four">Four</string> <string name="five">Five</string> <string name="six">Six</string> <string name="seven">Seven</string> <string name="eight">Eight</string> <string name="nine">Nine</string> <string name="ten">Ten</string> </resources>
package com.cyfang.scrollview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.Toast; public class ScrollView extends Activity { private CheckBox[] box = new CheckBox[10]; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { button = (Button) findViewById(R.id.button1); box[0] = (CheckBox) findViewById(R.id.checkBox1); box[1] = (CheckBox) findViewById(R.id.checkBox2); box[2] = (CheckBox) findViewById(R.id.checkBox3); box[3] = (CheckBox) findViewById(R.id.checkBox4); box[4] = (CheckBox) findViewById(R.id.checkBox5); box[5] = (CheckBox) findViewById(R.id.checkBox6); box[6] = (CheckBox) findViewById(R.id.checkBox7); box[7] = (CheckBox) findViewById(R.id.checkBox8); box[8] = (CheckBox) findViewById(R.id.checkBox9); box[9] = (CheckBox) findViewById(R.id.checkBox10); button.setOnClickListener(clickListener); } private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { String str = "你的名字有可能為:\n"; for (int i = 0; i < box.length; i++) { if (box[i].isChecked()) { str += box[i].getText() + "\n"; } } Toast.makeText(ScrollView.this, str, Toast.LENGTH_LONG).show(); } }; }
參考資料:
http://www.cnblogs.com/fbsk/archive/2011/10/12/2208333.html