我正在尝试使用Android cardslib并尝试使用本教程显示图像: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,这两个似乎都不起作用。提前致谢!

最佳答案

您必须使用CardThumbnailView.所在的卡片布局

例如,检查以下layout

要使用自定义布局,必须使用以下属性:card:card_layout_resourceID(必须使用自定义名称空间)

   <it.gmariotti.cardslib.library.view.CardViewNative
         card:card_layout_resourceID="@layout/native_card_thumbnail_layout" />

10-04 17:52