android - ScrollView 不工作 Android

标签 android android-scrollview

我已经实现了动态 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/

相关文章:

java - ScrollView 不随 onTouch 滚动

android - ScrollView 中的 NumberPicker

android - 如何在全 View 中显示图像

android - 如何从所选联系人中检索公司名称

android - 将许多大位图图像加载为缩略图 - Android

android - Android 无法识别 Firebase 'createCustomToken'

android - 旋转 ScrollView

java - Android 应用程序在 Pause() 上崩溃——逻辑在一个项目中工作但在另一个项目中失败

Android + Xamarin 推送通知自定义声音不会播放

android - 可扩展的 GridView 高度不会扩展到内容的高度