【发布时间】:2015-05-01 13:30:56
【问题描述】:
我在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>
这样一个过程的规范程序是什么?我已经尝试了很多调整,即:
- 创建第二个名为
cardslib_item_layout.xml的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 提供的贡献对我来说效果很好,尽管我后来发现,通过一些小的改动,也可以通过使用原始 cardlib 的
Card类来获得相同的结果,而无需创建新的DeviceCard类扩展Card类。我能够调整我的布局(标题和卡片的其余布局相互重叠,如屏幕截图所示),只需在
cardslib_item_layout.xml文件中进行一些细微的更改(我之前忽略了) ;同时,通过应用马里奥蒂对this 问题的回答,我能够消除自动附加到每张卡上的幻像填充。
【问题讨论】:
标签: android android-layout android-cardview cardslib