【问题标题】:How do I simply add image views to a LinearLayout dynamically如何简单地将图像视图动态添加到 LinearLayout
【发布时间】:2017-05-01 20:39:28
【问题描述】:

我从 SQLiteDatabase 获取字节数组并将它们转换为位图,然后将它们添加到图像视图中。我想将图像打印为垂直滚动的图像视图列表。但是,当我尝试添加视图时,它只会打印添加到 LinearLayout 的第一个图像,而不是其余的。

在 onCreate() 中

root = (LinearLayout) findViewById(R.id.photoFeedLayout);

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/photoFeedLayout"
    tools:context="com1032.cw2.ms01288.ms01288_assignment2.PhotoFeed">


</LinearLayout>

在我的 PhotoFeed.java 中。

public void run() {
    do {
        byte[] imData = images.getBlob(images.getColumnIndex("IMAGEDATA"));
        Bitmap bm = BitmapFactory.decodeByteArray(imData, 0, imData.length);

        LinearLayout.LayoutParams imParams =
                                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        ImageView iv = (ImageView) new ImageView(getApplicationContext());

        iv.setImageBitmap(bm);

        root.addView(iv,imParams);

        } while (images.moveToNext());
    }

有没有更好的方法通过将所有 ImageView 动态添加到根布局来垂直打印它们?

【问题讨论】:

  • 从 SQLiteDatabase 获取数组图像的列表视图,以便垂直显示

标签: android sqlite android-layout imageview


【解决方案1】:

在你的 layout.xml 文件中取一个 RecyclerView

然后在你的 OnCreate 方法里面初始化它并设置 Adpater 类。

现在创建适配器和类,并将图像数据传递到适配器内部。并将其设置为 ImageView。

这将显示所有的垂直..

更多详情请访问:

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

【讨论】:

  • 你能详细说明一下吗? “设置适配器类”..你是什么意思。我应该在适配器类中放什么。以及如何将适配器设置为图像视图?
猜你喜欢
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多