gpt4 book ai didi

android - ScrollView 不工作 Android

转载 作者:行者123 更新时间:2023-11-29 01:45:51 24 4
gpt4 key购买 nike

我已经实现了动态 TextView。我能够查看我动态生成的 TextView 。但是,我需要实现一个 ScrollView :

1.仅使用代码。

请帮忙。
我如何实现这两个功能?下面的代码工作正常(它获取所有 TextView 并动态显示在屏幕上但没有滚动功能)

TextView[] textViewArray = new TextView[iterator];

for( int i = 0; i < iterator; i++) {
textViewArray[i] = new TextView(narutoLinksOnly.this);
textViewArray[i].setText(narutoLinkHeadingName[i]);
textViewArray[i].setId(i);
textViewArray[i].setTextColor(0xff000000);
textViewArray[i].setTextSize(20);
textViewArray[i].setOnClickListener(this);

textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));//suggested
//textViewArray[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(textViewArray[i]);

}

InSide Oncreate:

linearLayout =  findViewById(R.id.dynamicTextview1);

XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dynamicTextview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Ivory"
android:orientation="vertical" >



</LinearLayout>

最佳答案

我在我的编辑器中用一些不同的值和名称再次尝试,但概念与您相同。

我的 Activity 类,即 MainActivity.java:

  public class MainActivity extends Activity implements OnClickListener {
ScrollView scrollView;
LinearLayout linearLayout;
String[] narutoLinkHeadingName = { "abcv", "bvvvv", "cvvvv", "dvvvv",
"avvvv", "bvvvv", "cvvvv", "d", "a", "b", "c", "d", "a", "b", "c",
"d", "avvvv", "b", "c", "d", "a", "vvvb", "c", "vvvvd", "a",
"vvvb", "cvvvv", "vvvvd" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

linearLayout = (LinearLayout) findViewById(R.id.dynamicTextview1);

scrollView = new ScrollView(MainActivity.this);
scrollView.setBackgroundColor(Color.BLUE);

TextView[] textViewArray = new TextView[narutoLinkHeadingName.length];

for (int i = 0; i < narutoLinkHeadingName.length; i++) {
textViewArray[i] = new TextView(MainActivity.this);
textViewArray[i].setText(narutoLinkHeadingName[i]);
textViewArray[i].setId(i);
textViewArray[i].setTextColor(0xff000000);
textViewArray[i].setTextSize(20);
textViewArray[i].setOnClickListener(this);
textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// suggested

((LinearLayout) linearLayout).addView(textViewArray[i]);
}
if(((ViewGroup)linearLayout.getParent()) != null){
((ViewGroup)linearLayout.getParent()).removeView(linearLayout);

scrollView.addView(linearLayout);
addContentView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
}else{
scrollView.addView(linearLayout);
addContentView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

}

我的布局即activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dynamicTextview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"
android:orientation="vertical" >
</LinearLayout>

现在它可以完美地垂直滚动,对于水平滚动,您可以使用 HorizontalScrollView在开发者网站上。

注意:我们必须注意 removeView() 方法,否则它可能会给出 IllegalStateException,例如 指定的子项已经有一个父项。您必须先对 child 的 parent 调用 removeView()

关于android - ScrollView 不工作 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586446/

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