如何动态地在TableLayout中绘制列

如何动态地在TableLayout中绘制列,就像它们在图像中一样。如何动态地在TableLayout中绘制列

我想通过一个类的对象来创建表。

回答:

我将与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

private TableLayout t1; 

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

现在我们

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

后置ATTR图像视图的实例我们会L个上路的ImageView的划船

tr.addView(img1); 

tr.addView(img2);

tr.addView(img3);

tr.addView(img4);

tr.addView(img5);

最后,我们将添加的TableRow到TableLayout

t1.addViwe(tr); 

没有运行该应用程序 当你点击链接,您将添加一行 你可以使用里面像循环验证码for循环或while循环 好运

以上是 如何动态地在TableLayout中绘制列 的全部内容, 来源链接: utcz.com/qa/265962.html

回到顶部