如何以编程方式创建Scrollview?

11 database android scrollview

我有一个表"TABLE_SUBJECT",其中包含许多主题.我需要
用Subject 创建一个水平滚动视图.

如何以编程方式创建包含数据库项的ScrollView?如果我输入1o主题,那么它将作为按钮显示在滚动视图中.可能吗?

waq*_*lam 22

你可以创建如下:

ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);
Run Code Online (Sandbox Code Playgroud)


小智 5

如果首先有很多元素,则需要包装并在“滚动”视图中添加;例如,我需要在滚动视图内部有很多文本视图,因此您需要创建ScrollView-> LinearLayout->许多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);
Run Code Online (Sandbox Code Playgroud)