【问题标题】:How to add image into TextView如何将图像添加到 TextView
【发布时间】:2014-12-02 21:44:57
【问题描述】:

我创建了一个表格,其中包含有关某些单独标题的信息。但是,我想知道如何将静态图像输入到列中的各个单元格中。

用于创建包含每个单元格中的图像的特定列的代码:

TextView label_col_1_datatab3 = new TextView(this);
label_col_1_datatab3.setId(200+cnt); 
label_col_1_datatab3.addTouchables(chair_list +count);  //THERE IS AN ERROR FOR THIS LINE WHERE I AM TRYING TO INPUT AN ARRAY OF IMAGES.
label_col_1_datatab3.setText(product_showcase_list[cnt]);
label_col_1_datatab3.setTextColor(Color.BLACK);
label_col_1_datatab3.setPadding(100, 50, 100, 50);
row_tab3.addView(label_col_1_datatab3);

chair_list 代码:

ArrayList<View> chair_list = new ArrayList {

        R.drawable.banana_chair, 
        R.drawable.circular_chair,
        R.drawable.purplechair
 };

XML:

             <TableRow
                 android:id="@+id/table_Row1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content">
                                <ListView
                                    android:id="@+id/list_image"
                                    android:layout_width="80dip"
                                    android:layout_height="80dip"
                                    android:src="@drawable/banana_chair"/>
                                <ListView
                                    android:id="@+id/list_image2"
                                    android:layout_width="80dip"
                                    android:layout_height="80dip"
                                    android:src="@drawable/circular_chair"/>
                                <ListView
                                    android:id="@+id/list_image3"
                                    android:layout_width="80dip"
                                    android:layout_height="80dip"
                                    android:src="@drawable/purplechair"/>
             </TableRow>

【问题讨论】:

  • 在您的 XML 文本视图中尝试 drawableright 或 drawabletop 或 drawableleft 或 drawableleft ??
  • @Dhina 你是什么意思?我已经更新了 XML 代码

标签: android listview android-listview arraylist


【解决方案1】:

试试下面的代码 sn-p :

TextView label_col_1_datatab3 = new TextView(this);
label_col_1_datatab3 .setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

【讨论】:

  • 但该参数仅适用于一个可绘制图标??如果我有多个怎么办?
  • @ErnestLee 只有一个可绘制对象,可以在文本下方/到末尾、左、右/开始/上方绘制。检查:developer.android.com/reference/android/widget/…, int, int, int)
  • 明白了,但是如果我需要在不同的单元格中添加不同的图像呢?
  • @ErnestLee,你说的是 TableLayout 的单元格吗?
  • 是的!原因“setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0)”仅将可绘制资源调用到表格布局中的一个单元格,但是如果我需要多个图像到不同的单元格怎么办?顺便说一句,谢谢,该代码本身可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2015-11-28
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 2021-11-16
  • 2011-08-07
相关资源
最近更新 更多