在文檔中寫明,CardView的BackgroundColor可以使用card_view:cardBackgroundColor
以XML格式設置。但是,我無法找到相應的方法來動態更改背景。使用mCardView.setBackgroundColor(Color.argb(75, 102,80,67));
將導致CardView失去圓角和陰影。動態更改CardView的BackgroundColor
4
A
回答
6
爲cardview的子類提供背景顏色將留下填充的部分,以防卡片視圖中有任何顏色,這不是一種好的方法。
假設您有一個適配器加載卡片視圖中的列表,動態更改卡片視圖的顏色如下。
在適配器的構造Viewholder類
mCardView = (CardView) itemView.findViewById(R.id.card_view);
在適配器類的onBindViewHolder方法:
holder.mCardView.setCardBackgroundColor(Color.GREEN); // will change the background color of the card view to green
如果持有人是你的viewholder類的對象。
1
我得到它通過設置CardView的孩子的背景很好地工作:
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:id="@+id/card_layout"
android:layout_width="match_parent"
android:layout_height="72dp"/>
</android.support.v7.widget.CardView>
然後
View cardLayout = mCardView.findViewById(R.id.card_layout);
cardLayout.setBackgroundColor(Color.GREEN);
-2
有將在即將發佈的API。目前,您唯一的選擇是使用XML。
+0
有沒有這方面的來源? – InsanityOnABun 2014-11-10 02:44:07
+0
請在API發佈時添加此解決方案。 – dasar 2014-12-12 13:26:49
相關問題
- 1. onClick backgroundColor更改
- 2. 更改TabControl的Backgroundcolor
- 3. 的backgroundColor更改JavaScript
- 4. 更改特定的UICell backgroundcolor
- 5. 如何動態更改CardView中的內容
- 6. Google Maps API V3 - 通過下拉列表動態更改InfoBox backgroundColor
- 7. CardView動態高度
- 8. 如何動態更改Cardview邊框顏色?
- 9. 更改SnackBar BackgroundColor EventBus異常
- 10. Android動態界面backgroundcolor
- 11. 從外部CSS的textArea更改backgroundcolor
- 12. 更改索引組件的UITableView「BackgroundColor」
- 13. 更改tabbarcontroller的導航欄backgroundcolor
- 14. WPF - 更改按鈕懸停上的backgroundcolor
- 15. 用PHP中的if語句更改backgroundcolor
- 16. 儘管滾動,更改ListView中單個視圖的Backgroundcolor
- 17. 重複更改backgroundColor和動畫的視圖
- 18. 開始其他活動後更改佈局的backgroundcolor
- 19. 如何更改BrowseFragment中的CardView高程
- 20. 動態更改
- 21. 動態更改
- 22. 動態更改
- 23. Datagrid - 如何更改前3行backgroundcolor
- 24. 我如何通過jquery更改backgroundcolor?
- 25. 如何使用Javascript更改3rd Div backgroundColor
- 26. 如何更改UIDatePicker backgroundColor?在Swift 3.0
- 27. 更改/刪除Achartengine標籤backgroundcolor
- 28. CardView內部的動態內容
- 29. Cardview的數據在滾動時更改RecyclerView
- 30. jQuery backgroundColor動畫
感謝它按預期工作。 – paradite 2014-10-16 09:29:47
只要cardview具有cardCornerRadius,它就無法按預期工作。仍在尋找更好的解決方案。 – manutudescends 2014-10-24 11:36:16
@manutudescends你確定嗎?它適用於我,我在我的xml中使用了'card_view:cardCornerRadius =「2dp」' – paradite 2014-10-24 18:32:42