Android studio以编程方式添加scrollview不会滚动

时间:2015-04-16 14:16:16

标签: android android-studio android-scrollview

我想用代码

创建scrollview

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

我应该怎么做才能滚动

您可以在链接http://screencast.com/t/bbDcDWoScPyM

后找到截图

这是Activity xml

<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);

1 个答案:

答案 0 :(得分:2)

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

    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));
    }