在ScrollView中添加CardView

时间:2018-06-05 12:40:17

标签: android scrollview cardview

在我的应用中,我有一个名为tasti.class的课程,其中我有一个简单的layout,其中包含ScrollViewCardView's

它还有一个按钮,当我按下它时,它会打开一个自定义AlertDialog所以我的问题是如何在我插入自定义{{1}后添加其他CardViews要放在AlertDialog的{​​{1}}和" Prezzo"中的名称它应该是TextView的描述,我的意思是按下按钮" AGGIUNGI"我怎么能创建一个新的CardView并将其添加到CardView的底部? enter image description here

这是布局部分的XML代码:

textView

虽然这是我ScrollView

的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:background="@color/blueLight"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".tasti">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/rellay_LOAD"
        android:paddingBottom="2dp">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alignmentMode="alignMargins"
            android:columnCount="2"
            android:columnOrderPreserved="false"
            android:rowCount="4">


            <!-- Row 1 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="4dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="CAFFE"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="4dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="CAPUCCINO"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!-- Row 2 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="HAMBURGER"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="BIRRA 50Cl"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!-- Row 3 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="PROFITTEROL"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="BIRRA 1l"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!-- Row 4 -->
            <!--Column 1 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="4dp"
                android:layout_marginEnd="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LIQUORI VARI"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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

            <!--Column 2 -->

            <android.support.v7.widget.CardView
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="4dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:layout_rowWeight="1"
                app:cardElevation="0.50dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="4dp"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LIMONCINO"
                        android:textAlignment="center"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />


                </LinearLayout>

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


        </GridLayout>


    </ScrollView>




    <RelativeLayout
        android:id="@+id/rellay_LOAD"
        android:layout_width="match_parent"
        android:layout_height="97dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_centerInParent="true"
        android:layout_weight="0.65"
        android:background="@color/colorWhite"
        android:clickable="true"
        android:gravity="center"
        android:focusable="true"
        android:foreground="?attr/selectableItemBackground"
        tools:ignore="NestedWeights,ObsoleteLayoutParam">




        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:text="CREATE"
            android:textSize="14sp"
            tools:ignore="HardcodedText" />


    </RelativeLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

在Kotlin,您可以这样做:

val mainLinearLayout = findViewById(R.id.constraintLayout) as LinearLayout
val cardLinearLayout = LinearLayout(this)
cardLinearLayout.orientation = LinearLayout.VERTICAL
val params = RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT)
params.setMargins(16,16,16,16)
val cardView = CardView(this)
cardView.radius = 15f
cardView.setCardBackgroundColor(Color.parseColor("#002388"))
cardView.setContentPadding(36,36,36,36)
cardView.layoutParams = params
cardView.cardElevation = 30f
val quote = TextView(this)
quote.text = "\"My cardview.\n";
quote.textSize = 24f
quote.setTextColor(Color.WHITE)
quote.setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL)
cardLinearLayout.addView(quote)
cardLinearLayout.addView(name)
cardView.addView(cardLinearLayout)
mainLinearLayout.addView(cardView)