mercredi 14 mai 2014

Androïd : ScrollView contenant un LinearLayout vertical :

Dans l'activity_main.xml :
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_below="@+id/editText"
        android:layout_above="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>


Dans le OnCreate du MainActivity.java :
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        for(int i=0;i<50;i++) {
            TextView textView = new TextView(this);
            textView.setText("inside_" + i);
            linearLayout.addView(textView);
        }