Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在滚动视图中添加CardView_Android_Scrollview_Cardview - Fatal编程技术网

Android 在滚动视图中添加CardView

Android 在滚动视图中添加CardView,android,scrollview,cardview,Android,Scrollview,Cardview,在我的应用程序中,我有一个名为tasti.class的类,其中我有一个简单的布局,其中有一个滚动视图和一些CardView的 它还有一个按钮,当我按下它时,它会打开一个自定义的AlertDialog,所以我的问题是,在我的自定义AlertDialog中插入要放置在CardView和“Prezzo”的TextView中的名称后,如何添加其他CardView它应该是文本视图的描述我的意思是按下按钮“AGGIUNGI”,如何创建新的CardView并将其添加到滚动视图的底部? 以下是布局部分的XML

在我的应用程序中,我有一个名为
tasti.class
的类,其中我有一个简单的
布局
,其中有一个
滚动视图
和一些
CardView的

它还有一个按钮,当我按下它时,它会打开一个自定义的
AlertDialog
,所以我的问题是,在我的自定义
AlertDialog
中插入要放置在
CardView
和“Prezzo”的
TextView
中的名称后,如何添加其他
CardView
它应该是
文本视图的描述
我的意思是按下按钮“AGGIUNGI”,如何创建新的CardView并将其添加到
滚动视图的底部

以下是布局部分的XML代码:

<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>

在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)

可能有帮助you@IonutJ.Bejan这就是我要找的谢谢!很好,快乐的编码:)这看起来不错,但我甚至不知道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)