2017-12-27 148 views

回答

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

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循環 好運

相關問題