Android - 动态向 ScrollView 中添加 ImageView

8

我对Android还比较陌生,需要一些代码方面的帮助。

我有一个硬币数组,想要在ScrollView中动态显示硬币的图片。每次用户添加硬币时,视图应该相应地更新。

谢谢!

以下是我想要实现的效果截图 - http://i.imgur.com/3l2fxKw.png


嗨,请查看此链接https://dev59.com/Y3RB5IYBdhLWcg3wtZMV - deadfish
3个回答

29

尝试这个样本

MainActivity

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>

谢谢,以下是翻译的文本:yah.感谢。这是我现在的代码-http://i.imgur.com/eRzN3Sb.png(颜色仅用于调试目的)。 最后一件事,我想能够为每个硬币从数组中加载适当的图像。基本上我想能够说coinView.setBackgroundResource(coinArray [x] .image) 我假设我需要以某种方式使用可绘制对象,我希望可以将其包含在我的枚举类中。 枚举类看起来像code(test) - Shmuel
我上面的评论已经没有空间了。我仍然需要一些帮助,在我的枚举类中使用可绘制对象。这是我的新问题链接 https://dev59.com/B2_Xa4cB1Zd3GeqPxyUN 谢谢 - Shmuel
尝试在空对象引用上调用虚拟方法,有任何想法为什么会这样? - Anitha

0

-3
使用GridView,并自定义适配器。GridView的工作方式类似于ListView。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接