2017-12-27 8 views

답변

1

당신은 이제 당신의 활동 작업 XML 파일에서 또한

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

을 폭 변경 버튼을 버튼

에 대한

를 온 클릭 ATTR 앉아 필요에 대한 매개 변수를 만들 나는 이미지 뷰 폭 match_parent와 함께 작동합니다 에서 onCreate 방식에 TableLayout을

private TableLayout t1; 

제조 T1 파라미터

01 23,516,의 onclick 방법 내부
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); 

선언 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을 설정하기 우리가 갈길 난

tr.addView(img1); 
tr.addView(img2); 
tr.addView(img3); 
tr.addView(img4); 
tr.addView(img5); 

마지막으로 우리는 당신이 같은 루프 내에서이 코드를 사용 할 수 TableLayout을

t1.addViwe(tr); 

에 당신이 버튼을 클릭하면 응용 프로그램 당신이 행 을 추가합니다 없음 실행 tableRow를 추가하지 않습니다 행하기 위해 이미지 뷰를 추가 for 루프 또는 while 루프 행운을 빌어 요.

관련 문제