5

レイアウトにスクロールビューを動的に追加するにはどうすればよいですか? そのスクロールビューを追加したら、その中にlinearlayoutを追加して、そのlinearlayoutにコントロールを追加できるようにしますか?

4

4 に答える 4

7

お役に立てば幸いです。

このコードを試してください...

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);

        ScrollView sv = new ScrollView(this);
        sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        ll.setOrientation(1);
        sv.addView(ll);

        for(int i = 0; i < 20; i++) 
        {
            Button b = new Button(this);
            b.setText("Button "+i);
            ll.addView(b);
        }

        rl.addView(sv);


        /* If you want to set entire layout as dynamically, then remove below lines in program :
         * setContentView(R.layout.activity_main);
         * RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
         * rl.addView(sv);
         * 
         * And Add below line :
         * this.setContentView(sv);

        */

    }
于 2012-08-03T06:07:06.050 に答える
5

これを試して

http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/

于 2012-08-03T05:35:08.410 に答える
-1

ウィジェットを ScrollView 内にラップできます。簡単な例を次に示します。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt"/>
    </LinearLayout>
</ScrollView>

コードで実行する場合:

ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);
于 2012-08-03T10:36:31.690 に答える
-1

これを試してみてください:

    TextView tv = new TextView(this);
    tv.setText(msg);       
    tv.setMovementMethod(new ScrollingMovementMethod());
    setContentView(tv);
于 2012-08-03T07:08:57.820 に答える