【问题标题】:ImageView does not show up when added to a TableRow programmatically以编程方式添加到 TableRow 时,ImageView 不显示
【发布时间】:2013-04-16 15:05:35
【问题描述】:

以下是相关代码位:

<HorizontalScrollView
        android:id="@+id/cards_scrollview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:fillViewport="false" >

        <TableRow
            android:id="@+id/cards_container"
            android:layout_width="wrap_content"
            android:layout_height="100dp" >

            <ImageView
                android:id="@+id/test_card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/cards_distance"
                android:adjustViewBounds="true"
                android:src="@drawable/card_back" />

        </TableRow>

</HorizontalScrollView>

private void addCard(Drawable d) {
        TableRow container = (TableRow) findViewById(R.id.cards_container);
        ImageView card = new ImageView(this);

        card.setAdjustViewBounds(true);
        card.setPadding(R.dimen.zero, R.dimen.zero, R.dimen.cards_distance, R.dimen.zero);
        TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        card.setLayoutParams(params);
        card.setImageDrawable(d);
        card.setVisibility(View.VISIBLE);

        container.addView(card);
}

我确信我要添加的 Drawable 不为空,边界设置正确,alpha 设置为 255,并且适合。我已经通过将它应用到 test_card 而不是尝试添加新的 ImageViews 来测试它,这非常有效。

这里有什么我遗漏的吗?

【问题讨论】:

  • 将 youractivty.this 传递给上下文
  • @edwin 方法无论如何都是顶级的,但我想通过 [Activity].this 是很好的风格。就我而言,没有任何改善。

标签: java android imageview tablerow


【解决方案1】:

在修改代码一个小时后,事实证明setPadding() 实际上将整数而不是资源引用作为参数,这就是把事情搞砸的原因。

所以基本上我需要做的就是改变:

card.setPadding(R.dimen.zero, R.dimen.zero, R.dimen.cards_distance, R.dimen.zero);

card.setPadding(0, 0, 5, 0);

【讨论】:

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