【问题标题】:What is the accurate way to customize cards with cardslib?使用cardlib自定义卡片的准确方法是什么?
【发布时间】: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


    【解决方案1】:

    试试这个
    您可以为卡片库定义自己的布局。

    创建您的自定义 XML:

    这是一个例子custom_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="8dp"
        android:paddingRight="6dp"
        android:paddingTop="7dp"
        android:paddingBottom="7dp"
        android:id="@+id/parentView">
    
    
        <TextView
            android:id="@+id/name"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="27"
            android:paddingRight="8dp"
            android:paddingLeft="8dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:text=""
            android:layout_gravity="center"
            android:editable="false"
           />
    
    </LinearLayout>
    

    在您的 JAVA 代码中为您希望使用的自定义卡片创建一个类:

        import it.gmariotti.cardslib.library.internal.Card;
        import it.gmariotti.cardslib.library.internal.ViewToClickToExpand;
    
    public class DeviceCard extends Card {
    
            private String IP;
            private String MAC;
            private String name;
            private Boolean reachable;
            private Boolean editable;
            Boolean markedFlag;
    
            public DeviceCard(Context context) {
                this(context, R.layout.custom_layout);
            }
    
            public DeviceCard(Context context,String param1,...,Type paramn) {
    
                this(context, R.layout.device_card);
                this.name = param1;
            }
    
    
    
            public DeviceCard(Context context, int innerLayout) {
                super(context, innerLayout);
                init();
                Log.d("myTag", "Init called");
            }
    
    
            private void init(){
    
    
            }
    
            @Override
            public void setupInnerViewElements(ViewGroup parent, final View view)          {
                Log.i("myTag","setupInnerView");
    
                final TextView nameBox = (TextView)view.findViewById(R.id.name);
              //edit name if required
    
            }
        }
    

    现在在您的 JAVA 代码中,当您需要使用卡片列表时:

    DeviceCard card = new DeviceCard(this, name);
    

    这个方法一直对我有用

    【讨论】:

    • 我也不知道具体是怎么回事,布局看起来和我的第一张截图一样,RelativeLayout 被压缩在一个更小的矩形中!我相信已经按照你告诉我的做了,但可能我有点冤枉你的例子?
    • 你不需要做你以前做过的事情。完全删除它们并检查一次
    • 稍微调整一下我的内部布局后它正在工作(=整体结构保持一致);但还有更多!内部布局似乎永远不会与 CardRecyclerView 卡片的布局对齐。这意味着它显示好像我在各个方向都添加了一些填充,但我没有!你知道为什么吗?如果您需要,我可以发布更多详细信息!
    • 是的,因为定义的基卡layoit 有一些填充。您可能主要是在底部获得更多填充?正确的。只需发布您现在得到的新图像。也许我可以提供更多帮助;-)
    • 我找到了解决最后一个隐藏填充问题的方法,方法是覆盖此处描述的尺寸值:stackoverflow.com/questions/29134528/… 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2016-09-24
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多