我尝试使用Android cardlib并尝试使用本教程显示图像:https://github.com/gabrielemariotti/cardslib/blob/master/doc/THUMBNAIL.md
然而,它没有显示图像,只是一张空卡。
我的活动代码是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a Card
Card card = new Card(this);
//Create thumbnail
CardThumbnail thumb = new CardThumbnail(this);
//Set URL resource
thumb.setUrlResource("https://lh5.googleusercontent.com/-N8bz9q4Kz0I/AAAAAAAAAAI/AAAAAAAAAAs/Icl2bQMyK7c/s265-c-k-no/photo.jpg");
//Add thumbnail to a card
card.addCardThumbnail(thumb);
//Set card in the cardView
CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
cardView.setCard(card);
}
我也试过从本地资源中执行此操作,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a Card
Card card = new Card(this);
//Create thumbnail
CardThumbnail thumb = new CardThumbnail(this);
//Set ID resource
thumb.setDrawableResource(R.drawable.ic_launcher);
//Add thumbnail to a card
card.addCardThumbnail(thumb);
//Set card in the cardView
CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
cardView.setCard(card);
我的XML是:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<it.gmariotti.cardslib.library.view.CardViewNative
android:id="@+id/carddemo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp"/>
<it.gmariotti.cardslib.library.view.CardViewNative
card_layout_resourceID="@layout/native_card_thumbnail_layout"
android:layout_below="@id/carddemo"
android:id="@+id/carddemo_thumb_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
我做错了什么?为什么我的图像无法加载?在我的XML文件中,无论是carddemo还是carddemo_thumb_url,哪一张卡都指向它似乎无关紧要。提前谢谢!
答案 0 :(得分:1)
您必须使用卡片布局,其中有CardThumbnailView.
例如,请检查此layout:
要使用自定义布局,您必须使用以下属性:card:card_layout_resourceID
(您必须使用自定义命名空间)
<it.gmariotti.cardslib.library.view.CardViewNative
card:card_layout_resourceID="@layout/native_card_thumbnail_layout" />