Android 工作室以编程方式添加滚动视图不滚动

Android studio add scrollview programmatically doesn't scroll

我想用代码创建滚动视图,

我已经创建了 scrollview 和 linearlayout,但是没有滚动到底部,

我应该怎么做才能滚动

你可以在linkhttp://screencast.com/t/bbDcDWoScPyM

下面找到截图

这里是Activityxml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/rlt"
                android:theme="@style/Theme.AppCompat.Light.NoActionBar"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true"
                tools:context="com.test.test.test">
</RelativeLayout>

这里是 java 代码

RelativeLayout lL = (RelativeLayout) findViewById(R.id.rlt);

        ScrollView sv= new ScrollView(this);//(ScrollView) findViewById(R.id.svScroll);

        LinearLayout sahne = new LinearLayout(this);

        sahne.setOrientation(LinearLayout.VERTICAL);

        txt1.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt2.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt3.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt4.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt5.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt6.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));
        txt7.setLayoutParams(new LinearLayout.LayoutParams(textRequireWidth,textRequireHeight));

        sahne.addView(txt1);
        sahne.addView(txt2);
        sahne.addView(txt3);
        sahne.addView(txt4);
        sahne.addView(txt5);
        sahne.addView(txt6);
        sahne.addView(txt7);
        sahne.addView(btn1);

        sv.addView(sahne);

        lL.addView(sv);

我更改了您的代码的一些顺序并编写了以下演示。让我们试试看:

    RelativeLayout lL = (RelativeLayout) findViewById(R.id.rlt);

    ScrollView sv= new ScrollView(this);//(ScrollView) findViewById(R.id.svScroll);
    sv.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,ScrollView.LayoutParams.MATCH_PARENT));
    lL.addView(sv);

    LinearLayout sahne = new LinearLayout(this);
    sahne.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    sahne.setOrientation(LinearLayout.VERTICAL);
    sv.addView(sahne);
    TextView[] textViews = new TextView[20];
    for(int i = 0; i < 20; i++){
        textViews[i] = new TextView(this);
        textViews[i].setLines(2);
        textViews[i].setText("Bla bla bla bla");
        textViews[i].setGravity(Gravity.CENTER);
        textViews[i].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        sahne.addView(textViews[i], i, new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    }