簡體   English   中英

以編程方式添加到TableRow時,ImageView不顯示

[英]ImageView does not show up when added to a TableRow programmatically

以下是相關的代碼位:

<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進行了測試,而不是嘗試添加新的ImageViews(效果很好)。

我在這里想念什么嗎?

經過一小時的代碼修改,結果發現setPadding()實際上將ints而不是資源引用作為參數,這就是搞砸了。

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM