我有一张名为“TABLE_SUBJECT”的表,其中包含许多科目。我需要创建一个水平滚动视图来显示这些科目。
如何以编程方式创建一个带有数据库项的ScrollView?如果我输入10个科目,则它们会出现在滚动视图中作为按钮。这可能吗?
我有一张名为“TABLE_SUBJECT”的表,其中包含许多科目。我需要创建一个水平滚动视图来显示这些科目。
如何以编程方式创建一个带有数据库项的ScrollView?如果我输入10个科目,则它们会出现在滚动视图中作为按钮。这可能吗?
您可以按以下方式创建它:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);
如果你有很多元素,首先需要将它们包装起来并添加到滚动视图中;例如,我需要在滚动视图中放置许多文本视图,因此需要创建ScrollView->LinearLayout->Many textview。
ScrollView scrollView = new ScrollView(context);
scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textView = new TextView(context);
textView.setText("my text");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.RIGHT);
linearLayout.addView(textView);
scrollView.addView(linearLayout);
这可能会对您有所帮助。
HorizontalScrollView hsrll = (HorizontalScrollView)findViewById(R.id.hrsll);
b = new Button(this);
for (int i = 0; i < 5; i++) {
b.setWidth(LayoutParams.WRAP_CONTENT);
b.setHeight(LayoutParams.WRAP_CONTENT);
b.setText("b"+i);
b.setId(100+i);
hsrll.addView(b);
}
不要使用for循环,根据您的需要修改代码(数据库中的记录数)。但这是用于动态创建按钮的代码。
我是这样做的:
对我来说效果很好。
val scroll = ScrollView(context)
scroll.setBackgroundColor(R.color.transparent)
scroll.layoutParams = LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
)
scroll.addView(yourTableView)
LayoutParams
? - Johnny FiveViewGroup
在这里应该可以胜任。 - waqaslam