【问题标题】:Android - Dynamically add an image view to a ScrollViewAndroid - 将图像视图动态添加到 ScrollView
【发布时间】:2013-03-01 08:09:53
【问题描述】:

我是 Android 新手,需要一些代码方面的帮助。

基本上我有一个硬币数组,我想在 ScrollView 中动态显示硬币的图像。每次用户添加硬币时,视图都应相应更新。

谢谢!

这是我想做的截图 - http://i.imgur.com/3l2fxKw.png

【问题讨论】:

标签: android dynamic scrollview


【解决方案1】:

试试这个示例

主要活动

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>

【讨论】:

  • 是的。谢谢。这就是我现在拥有的 -i.imgur.com/eRzN3Sb.png(颜色仅用于调试目的。)最后一件事,我希望能够从数组中为每个硬币加载适当的图像。基本上我想能够说 coinView.setBackgroundResource(coinArray[x].image) 我假设我需要以某种方式使用可绘制对象,我希望它可以包含在我的枚举类中。枚举类看起来像 code(test)
  • 我在上面的评论中用完了空间。我仍然需要在我的枚举类中使用可绘制对象的一些帮助。这是我的新问题stackoverflow.com/questions/15182045/…的链接,谢谢
  • “尝试在空对象引用上调用虚拟方法”知道为什么吗?
【解决方案2】:

考虑使用SurfaceView

您将有一个画布对象来绘制您的图像,然后对齐硬币只是适当地映射您的像素的问题。

这是一个很好的教程: http://www.mindfiresolutions.com/Using-Surface-View-for-Android-1659.php

【讨论】:

    【解决方案3】:

    改用GridView 并自定义您自己的适配器。 Gridview 像 ListView 一样工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多