【发布时间】:2012-03-27 10:33:02
【问题描述】:
我有一张表“TABLE_SUBJECT”,其中包含许多主题。我需要创建
一个带有主题的水平滚动视图。
如何以编程方式创建带有数据库项的 ScrollView?如果我输入 1o 主题,那么它将作为一个按钮出现在滚动视图中。有可能吗?
【问题讨论】:
标签: android database scrollview
我有一张表“TABLE_SUBJECT”,其中包含许多主题。我需要创建
一个带有主题的水平滚动视图。
如何以编程方式创建带有数据库项的 ScrollView?如果我输入 1o 主题,那么它将作为一个按钮出现在滚动视图中。有可能吗?
【问题讨论】:
标签: android database scrollview
您可以按如下方式创建它:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);
【讨论】:
LayoutParams?
ViewGroup 在这里应该没问题
如果你首先有很多元素,你需要在滚动视图中进行总结和添加;例如,我需要在滚动视图中添加多个文本视图,因此您需要创建 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 循环,只需根据需要修改代码(数据库中没有记录)。但这是动态创建按钮的代码。
【讨论】:
我是这样做的:
对我来说效果很好。
【讨论】:
在 Kotlin 中你可以使用下面的代码
val scroll = ScrollView(context)
scroll.setBackgroundColor(R.color.transparent)
scroll.layoutParams = LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
)
scroll.addView(yourTableView)
【讨论】: