本例為一個自訂的UI控制項InternalSelectionView 添加捲軸,InternalSelectionView 可以顯示一個矩形列表,矩形的寬度為View的寬度,允許自訂欄表的行數,矩形的高度為View的高度平分為列表的行數。 參見Android ApiDemos樣本解析(118):Views->Focus->4. Internal Selection。
本例在代碼中將InternalSelectionView 的高度設為螢幕高度的兩倍,確保ScrollView可以滾動:
[java]
InternalSelectionView isv
= new InternalSelectionView(this, 10);
int screenHeight
= getWindowManager().getDefaultDisplay().getHeight();
LinearLayout.LayoutParams llLp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
2 * screenHeight); // 2x screen height to ensure scrolling
isv.setLayoutParams(llLp);
ll.addView(isv);
InternalSelectionView isv
= new InternalSelectionView(this, 10);
int screenHeight
= getWindowManager().getDefaultDisplay().getHeight();
LinearLayout.LayoutParams llLp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
2 * screenHeight); // 2x screen height to ensure scrolling
isv.setLayoutParams(llLp);
ll.addView(isv);
ScrollView 自動管理滾動功能,如果它檢測到其內部的內容的高度大於螢幕高度,將自動支援滾動功能: