答案 0 :(得分:1)
我将使用ImageView宽度match_parent 您需要更改宽度
<TableLayout
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/qr"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/qr"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/qr"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/qr"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/qr"/>
</TableRow>
</TableLayout>
同样在您的XML文件制作按钮并坐在onClick attr按钮
现在使用tableLayout的Activity make参数
private TableLayout t1;
on onCreate方法准备t1参数
t1 = (TableLayout)findViewById(R.id.t1);
t1.setColumnStretchable(0,true);
t1.setColumnStretchable(1,true);
t1.setColumnStretchable(2,true);
t1.setColumnStretchable(3,true);
t1.setColumnStretchable(4,true);
在onClick方法中 声明TableRow
//you can declare Context with parameter and used instead of this
TableRow tr = new TableRow(this);
现在我们制作图像视图的实例
ImageView img1 = new ImageView(this);
ImageView img2 = new ImageView(this);
ImageView img3 = new ImageView(this);
ImageView img4 = new ImageView(this);
ImageView img5 = new ImageView(this);
现在我们设置attr作为图像视图的例子
img1.setImageResource(R.drawable.ic_favorite);
img1.setMaxWidth(50);
img1.setMinimumWidth(30);
img1.setMaxHeight(50);
img1.setMinimumHeight(30);
img1.setForegroundGravity(Gravity.CENTER); // this is work from API23
img2.setImageResource(R.drawable.ic_favorite);
img2.setMaxWidth(50);
img2.setMinimumWidth(30);
img2.setMaxHeight(50);
img2.setMinimumHeight(30);
img2.setForegroundGravity(Gravity.CENTER); // this is work from API23
img3.setImageResource(R.drawable.ic_favorite);
img3.setMaxWidth(50);
img3.setMinimumWidth(30);
img3.setMaxHeight(50);
img3.setMinimumHeight(30);
img3.setForegroundGravity(Gravity.CENTER); // this is work from API23
img4.setImageResource(R.drawable.ic_favorite);
img4.setMaxWidth(50);
img4.setMinimumWidth(30);
img4.setMaxHeight(50);
img4.setMinimumHeight(30);
img4.setForegroundGravity(Gravity.CENTER); // this is work from API23
img5.setImageResource(R.drawable.ic_favorite);
img5.setMaxWidth(50);
img5.setMinimumWidth(30);
img5.setMaxHeight(50);
img5.setMinimumHeight(30);
img5.setForegroundGravity(Gravity.CENTER); // this is work from API23
之后我们将imageView添加到行
tr.addView(img1);
tr.addView(img2);
tr.addView(img3);
tr.addView(img4);
tr.addView(img5);
最后我们将tableRow添加到TableLayout
t1.addViwe(tr);
没有运行应用程序 当您单击按钮时,您将添加行 你可以在循环中使用这个代码,如for循环或while循环 祝你好运