web-dev-qa-db-ja.com

Android-画像ビューをScrollViewに動的に追加します

私はAndroidにかなり慣れていないので、コードについて少し助けが必要です。

基本的に私はコインの配列を持っており、ScrollViewにコインの画像を動的に表示したいと思います。ユーザーがコインを追加するたびに、ビューは適切に更新されます。

ありがとうございました!

これが私がやりたいことのスクリーンショットです- http://i.imgur.com/3l2fxKw.png

8
Shmuel

このサンプルを試してください

主な活動

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);
    LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
    for(int x=0;x<3;x++) {
        ImageView image = new ImageView(MainActivity.this);
        image.setBackgroundResource(R.drawable.ic_launcher);
        linearLayout1.addView(image);
    }
}

activity_main.xml

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical"
    tools:context=".MainActivity" >

    <ScrollView
        Android:id="@+id/scrollView1"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content" >

        <LinearLayout
        Android:id="@+id/linearLayout1"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent" >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
28
NaviRamyle

SurfaceView の使用を検討してください。

画像を描画するためのキャンバスオブジェクトがあり、コインの位置合わせはピクセルを適切にマッピングするだけです。

これは良いチュートリアルです: http://www.mindfiresolutions.com/Using-Surface-View-for-Android-1659.php

0
l46kok