滚动视图不适用于Android

时间:2014-02-05 19:17:13

标签: android android-scrollview

我已经实现了Dynamic TextViews。我能够查看动态生成的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>

2 个答案:

答案 0 :(得分:1)

我在编辑器中再次尝试了一些不同的值和名称,但概念和你一样。

我的活动类,即 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 ,如指定的子级已经有父级。您必须首先在孩子的父母上调用removeView()

答案 1 :(得分:0)

将你的linearlayout放在xml中的scrollview中。

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



</LinearLayout>
</ScrollView>
相关问题