Android ApiDemos示范解析(155):Views->Layouts->ScrollView->3. Internal Selection

Android ApiDemos示例解析(155):Views->Layouts->ScrollView->3. Internal Selection

本例为一个自定义的UI控件InternalSelectionView 添加滚动条,InternalSelectionView 可以显示一个矩形列表,矩形的宽度为View的宽度,允许自定义列表的行数,矩形的高度为View的高度平分为列表的行数。 参见Android ApiDemos示例解析(118):Views->Focus->4. Internal Selection。

本例在代码中将InternalSelectionView 的高度设为屏幕高度的两倍,确保ScrollView可以滚动:

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 自动管理滚动功能,如果它检测到其内部的内容的高度大于屏幕高度,将自动支持滚动功能:

Android ApiDemos示范解析(155):Views->Layouts->ScrollView->3. Internal Selection