【问题标题】:Android studio add scrollview programmatically doesn't scrollAndroid工作室以编程方式添加滚动视图不滚动
【发布时间】:2015-04-16 14:16:15
【问题描述】:

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

我已经创建了滚动视图和线性布局,但没有滚动到底部,

我应该怎么做才能滚动

你可以在下面的链接http://screencast.com/t/bbDcDWoScPyM找到截图

这里是活动 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);

【问题讨论】:

  • 您是否尝试在ScrollViewLinearLayout 上设置布局参数?
  • 你试过为 ScrollView sv 和 LinearLayout sahne 设置 layout_width 和 layout_height 吗?我相信这些参数是必需的。
  • 可以,但不起作用

标签: android android-studio android-scrollview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多