2014-02-05 42 views
2

我已经实现了Dynamic TextViews。我能够查看我的动态生成的文本视图。但是,我需要实现滚动视图:滚动视图无法使用Android

1. 仅使用代码。

请帮忙。
我如何实现这2个功能?下面 代码工作就好了(它会在屏幕上的所有的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]); 

       } 

里面的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> 
+1

把你的xml中的滚动视图里面的linearlayout放在里面.. – Ranjit

+0

@RanjitPati是的它的工作方式:)。但是,我们如何使用代码实现相同的功能,而不是通过在xml中定义相同的代码。 – SeasonalShot

+0

移动xmlns:android =“http://schemas.android.com/apk/res/android”滚动查看布局的父元素 –

回答

1

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

我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异常指定的孩子已经有一个父。你必须先调用孩子父母的removeView()

+0

滚动视图应该match_parent,而不是wrap_content,另一方面,TextView必须wrap_content(以允许滚动视图在onMeasure和onLayout期间测量大小)。最后,不要使用已弃用的FILL_PARENT。 –

+0

感谢Martin ... – Ranjit

+0

@Ranjit当你在做scrollView.addView(linearLayout);你把scrollview插入线性布局,反之亦然? – SeasonalShot

0

只需在scrollview中将您的linearlayout放入xml即可。

<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> 
+0

其实我编辑了我的问题。我只需要使用代码来实现这一点.BTW你的建议的方式工作,但我正在寻找实现相同的使用代码。 – SeasonalShot

+0

当您将任何视图添加到scrollView时,其子布局高度必须为wrap_content。 – Piyush