我在文件中声明了一张卡片cardslib_item_card_view
:
<it.gmariotti.cardslib.library.view.CardViewNative
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
style="@style/native_recyclerview_card.base"
android:id="@+id/carddemo"
android:layout_width="match_parent" android:layout_height="wrap_content">
并在方法中设置为内容视图onCreate()
:
public class CardMariotti extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardslib_item_card_view);
//Create a Card
Card card = new Card(this);
CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
cardView.setCard(card);
card.setOnClickListener(new Card.OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(CardMariotti.this, "Clickable card", Toast.LENGTH_LONG).show();
}
});
}
现在,我想用我自己的布局自定义它,包含一个窄标题和一些信息,如下所示:
<RelativeLayout android:id="@+id/cardlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:clickable="true">
<!-- layout containing 3 TextView -->
</RelativeLayout>
这种过程的规范程序是什么?我已经尝试了很多调整,即:
- 创建第二个 xml 文件,并以这种方式使用 的构造函数
cardslib_item_layout.xml
引用它:然后设置Card
Card card = new Card(this, R.layout.cardslib_item_layout);
setContentView(R.layout.cardslib_item_card_view)
- 在卡片内附加布局,然后设置
setContentView(R.layout.cardslib_item_card_view)
.
这边走; cardslib_item_card_view
:
<it.gmariotti.cardslib.library.view.CardViewNative
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
android:id="@+id/carddemo"
android:layout_width="match_parent" android:layout_height="wrap_content">
<RelativeLayout>
<!-- my layout containing a header and some TextViews -->
</RelativeLayout>
</it.gmariotti.cardslib.library.view.CardViewNative>
在这两个测试中,我都遇到了以下问题:
- 整体结果完全失真
- 最重要的是,RelativeLayout 放置在卡片的顶部,无法对卡片进行任何操作(例如,
Card.OnCardClickListener
在卡片本身上设置 将不起作用,因为用户将单击 RelativeLayout 而不是卡片本身)
尝试 1:
尝试 2:
什么是规范程序?
EDIT2:答案
@Msk 提供的贡献对我来说很好,尽管我后来发现,通过一些小的改动,也可以通过使用原始的 cardslib 的
Card
类来获得相同的结果,而不需要创建一个DeviceCard
扩展类的新Card
类。我能够调整我的布局(标题和卡片布局的其余部分相互重叠,如屏幕截图所示),只需在
cardslib_item_layout.xml
文件中进行一些微小且微不足道的更改(我之前忽略了);同时,通过应用马里奥蒂对这个问题的回答,我能够消除自动附加到每张卡上的幻像填充。