以编程方式添加到TableRow时,ImageView不会显示

时间:2013-04-16 15:05:35

标签: java android imageview tablerow

以下是相关的代码位:

<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不是null,正确设置边界,alpha设置为255,并且适合。我已经通过将它应用于test_card而不是尝试添加新的ImageView来测试它,它可以很好地工作。

这里有什么我想念的吗?

1 个答案:

答案 0 :(得分:1)

在对代码进行了一个小时的修补之后,事实证明setPadding()实际上采用了内注而不是资源引用作为参数,这就是弄乱它的所有内容。

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

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

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

相关问题