gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:20:28 24 4
gpt4 key购买 nike

我想用代码创建 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);

最佳答案

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

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

关于Android studio 以编程方式添加 scrollview 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29677440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com