在androidstudiojava中动态添加行
我想通过点击按钮添加一个新行,我找到了很多谷歌资源,但我不能这样做 activity_main.xml在androidstudiojava中动态添加行,java,android,xml,Java,Android,Xml,我想通过点击按钮添加一个新行,我找到了很多谷歌资源,但我不能这样做 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/NameTextView"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:layout_marginLeft="20sp"
android:layout_marginTop="10sp"
android:text="Name"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold" />
<EditText
android:id="@+id/PersonName"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:layout_marginLeft="20sp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mobile Number"
android:layout_marginLeft="20sp"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold" />
<EditText
android:id="@+id/PersonPhone"
android:layout_width="325dp"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginLeft="20sp"
android:hint="Mobile number"
android:inputType="phone" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address"
android:layout_marginLeft="20sp"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold" />
<EditText
android:id="@+id/PersonAddress"
android:layout_width="328dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:ems="10"
android:hint="Delivery Address"
android:inputType="textPostalAddress" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:text=" Order Details"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TableLayout
android:id="@+id/MainTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:stretchColumns="0,1,2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#000000"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=" Date "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="Item"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="Quantity"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/AddButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/DoneButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
我需要在单击“添加”按钮时动态添加多行,我正在使用java中的button.setOnClickListener,但我一直在创建新行。如果有人可以的话,请帮助我。还有一件事,我有另一个帐户,它显示我已经达到了发布问题的极限,我可以知道这个极限以及为什么它会显示,因为我只发布了大约4-5个问题。在添加按钮的onClickListner中,实现以编程方式添加TableRow及其元素的代码 JAVA部分
TableLayout tableLayout;
TableLayout tableLayout = (TableLayout) findViewById(R.id.MainTable);
为新行创建表格行标题
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
将数据添加到行中
我将向创建的TableRow添加两个文本视图,您可以根据需要添加任意数量的文本视图
TextView label_hello = new TextView(this);
label_hello.setId(20); // define id that must be unique
label_hello.setText("HELLO"); // set the text
label_hello.setTextColor(Color.WHITE);
label_hello.setPadding(5, 5, 5, 5);
TextView label_android = new TextView(this);
label_android.setId(21);// define id that must be unique
label_android.setText("ANDROID..!!"); // set the textlabel_android.setTextColor(Color.WHITE); // set the color
label_android.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_hello);// add the column to the table row
tr_head.addView(label_android);
将列添加到表行之后,是时候将表行添加到我们在开始时获取的主表布局了
tableLayout.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.MATCH_CONTENT));
建议在这种情况下,建议将RecyclerView与自定义布局一起使用。如果存在大量数据,您正在实施的方法将导致严重的性能问题。此外,使用RecyclerView可以使您添加/读取/删除数据的方式变得更加简单。在“订单详细信息”中的空格是有目的的。tru head.setBackgroundColor(Color.GRAY)?你能解释一下回收者的观点吗?SetBackgroundColor不是管理性的,请通读这篇文章了解一下回收者的观点。请也支持我的问题