我在Android应用中使用CardsLib库。我试图使用此库中的MaterialCard。但是,当我试图运行示例
时,我得到了这个输出
以下是此活动的代码:
MaterialActivity.java
package com.a5corp.weather;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import it.gmariotti.cardslib.library.cards.actions.BaseSupplementalAction;
import it.gmariotti.cardslib.library.cards.actions.TextSupplementalAction;
import it.gmariotti.cardslib.library.cards.material.MaterialLargeImageCard;
import it.gmariotti.cardslib.library.internal.Card;
public class MaterialActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_material);
ArrayList<BaseSupplementalAction> actions = new ArrayList<>();
// Set supplemental actions
TextSupplementalAction t1 = new TextSupplementalAction(this, R.id.text1);
t1.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getApplicationContext()," Click on Text SHARE ",Toast.LENGTH_SHORT).show();
}
});
actions.add(t1);
TextSupplementalAction t2 = new TextSupplementalAction(this, R.id.text2);
t2.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getApplicationContext()," Click on Text LEARN ",Toast.LENGTH_SHORT).show();
}
});
actions.add(t2);
//Create a Card, set the title over the image and set the thumbnail
MaterialLargeImageCard card =
MaterialLargeImageCard.with(this)
.setTextOverImage("Italian Beaches")
.setTitle("This is my favorite local beach")
.setSubTitle("A wonderful place")
.setupSupplementalActions(R.layout.carddemo_native_material_supplemental_actions_large_icon, actions)
.build();
card.setOnClickListener(new Card.OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getApplicationContext()," Click on ActionArea ",Toast.LENGTH_SHORT).show();
}
});
}
}
activity_material.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_material"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.a5corp.weather.MaterialActivity">
<it.gmariotti.cardslib.library.view.CardViewNative
android:id="@+id/carddemo_largeimage_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card:card_layout_resourceID="@layout/native_material_largeimage_text_card"
style="@style/card_external"
/>
carddemo_native_material_supplemental_actions_large_icon.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/actions_padding"
android:paddingLeft="@dimen/actions_padding_left"
android:paddingRight="@dimen/actions_padding_left">
<TextView
android:id="@+id/text1"
android:text="SHARE"
android:background="?android:selectableItemBackground"
android:layout_width="wrap_content"
style="@style/card.native.actions"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:text="LEARN MORE"
android:background="?android:selectableItemBackground"
android:textColor="#FF9800"
android:layout_width="wrap_content"
style="@style/card.native.actions"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:1)
哈哈,在Java代码之后只需要添加两行:
CardViewNative cardViewNative = findViewById(R.id.carddemo_largeimage_text);
cardViewNative.setCard(card);