android动态添加cardview

时间:2018-10-15 05:10:50

标签: android android-cardview

首先我有数组索引是2,然后像这样添加2 cardview enter image description here

接下来,如果arrayindex为3,则像这样enter image description here

添加

如何创建此布局?

1 个答案:

答案 0 :(得分:2)

请看下面的代码

将此代码添加到您的onCreate()

 mLayout = findViewById(R.id.layout);// your main xml ViewGroup like Linear or Relative or any other.
        int id=2;//use this varibale to identified your array list or array

        if(id==2)
            addTwoCard();
        else
            addThreeCard(); 

将这些添加到您的班级中。

private void addThreeCard() {
        View view = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.two_child, null);
        mLayout.addView(view);
    }

    private void addTwoCard() {
        View view = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.three_child, null);
        mLayout.addView(view);
    }

以及您项目中的这些XML

two_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:padding="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:elevation="10dp"
        android:layout_height="100dp"/>

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:padding="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:elevation="10dp"
        android:layout_height="100dp"/>


</LinearLayout>

输出:-enter image description here

three_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:padding="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:elevation="10dp"
        android:layout_height="100dp"/>

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:padding="20dp"
            android:layout_marginTop="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:elevation="10dp"
            android:layout_height="100dp"/>


    </LinearLayout>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:padding="20dp"
        android:elevation="10dp"
        android:layout_margin="40dp"
        android:layout_height="100dp"/>



</LinearLayout>

输出:-enter image description here

希望您的问题得到解决。...

相关问题