2

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

4

1 回答 1

1

您必须使用卡片布局,其中有CardThumbnailView.

例如检查这个布局

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

   <it.gmariotti.cardslib.library.view.CardViewNative
         card:card_layout_resourceID="@layout/native_card_thumbnail_layout" />
于 2014-12-22T07:39:22.233 回答