Android setContentview(scroll) 隐藏后面的ConstraintLayout对象

Android setContentview(scroll) hides the ConstraintLayout objects at the back

我是 android 的新手,我创建了一个 ScrollView 并在其中添加了一个包含 TextView 和选择器的垂直线性布局。所有这些都是以编程方式创建的。我的问题出在 setContentView(scroll) 行上。它似乎涵盖了我在后面的 ConstraintLayout 上创建(不是以编程方式)的所有对象。但是我已经将我的动态ScrollView的高度设置为800。我如何显示ScrollView后面的4个按钮?

查看截图:

non dynamic objects in constraintlayout

programmatically created objects in linearlayout inside scrollview

这是代码。

        ScrollView scroll = new ScrollView(this);
        
        scroll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                800));
        scroll.setFillViewport(true);
      
        setContentView(scroll); //here is the line with issue

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(lp1);

        scroll.addView(linearLayout);
        
        for( int i = 0; i < res2.getCount(); i++ )
        {
            NumberPicker numberPicker = new NumberPicker(this);
            numberPicker.setMinValue(0);
            numberPicker.setMaxValue(100);
            TextView textView = new TextView(this);
            textView.setText(/*textArray[i] + " " +*/ res2.getString(1));
            linearLayout.addView(textView);
            linearLayout.addView(numberPicker);

            res2.moveToNext();
        }

提前感谢您的帮助。

在 xml 上声明您的 LinearLayout,而不是以编程方式添加它。您将始终拥有线性视图和滚动视图。然后像在 for 循环中所做的那样用所需的视图填充线性。这样您就可以确保您的视图位于正确的位置和正确的大小。