以编程方式在 CardView 中创建 RelativeView android studio

Create RelativeView inside CardView programmatically android studio

我正在尝试为我的应用程序创建历史记录 activity 页面。我使用用户数据从 sql table 获取数据,并将其设置到卡片中。我已经创建了历史记录 activity 中的第一张卡片,我不想预先创建所有卡片,但我不知道如何以编程方式添加更多卡片视图。我在卡片视图中有一个相对布局,所以我可以按我想要的方式组织数据。这是我的 xml 布局 activity

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"    
     android:layout_width="match_parent"
     xmlns:ads="http://schemas.android.com/apk/res-auto"
     xmlns:card_view="http://schemas.android.com/apk/res-auto"
     android:layout_height="match_parent"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     android:paddingBottom="@dimen/activity_vertical_margin">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear History"
        android:id="@+id/clearSQLite"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:width="150dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tips"
        android:id="@+id/TipsButton"
        android:width="150dp"
        android:layout_above="@+id/adView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <android.support.v7.widget.CardView
        tools:context="com.winansbros.soccerpredictor.History"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:id="@+id/team1imageview"
                android:layout_alignParentTop="true"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/team1name"
                android:layout_centerVertical="true"
                android:layout_below="@+id/team2name"
                android:layout_toRightOf="@+id/team1imageview"
                android:layout_toEndOf="@+id/team1imageview"
                android:width="100dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/card1score"
                android:width="20dp"
                android:layout_centerVertical="true"
                android:layout_alignTop="@+id/team1name"
                android:layout_toRightOf="@+id/team1name"
                android:layout_toEndOf="@+id/team1name"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/team2name"
                android:width="100dp"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/team2imageview"
                android:layout_toStartOf="@+id/team2imageview"/>
            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:id="@+id/team2imageview"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

 </RelativeLayout>

如何创建我在 java 中的卡片视图布局的另一个版本? 提前谢谢你

查看 RecyclerView. If you set it up right, it uses the card as a template and will create cards based off of an array of data (which is customizeable.) This 教程应该会有帮助。