2014-02-05 62 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