본문 바로가기

프로그램/ANDROID

안드로이드 테이블 로우 여러버튼 넣기

반응형

안드로이드 테이블 로우 여러버튼 넣기

 

자바 소스에서 TableRow 안에 여러버튼을 넣다보면 갯수가 많으면 버튼이 다 들어가지 않습니다.

 

그래서 삽질을 하다 겨우 찾았습니다.

 

화면길이에 따라 자동으로 갯수만큼 알아서 크기가 조절되서 들어갑니다.

       

final TableLayout tableLayout = (TableLayout) findViewById(R.id.table); 
tableLayout.setShrinkAllColumns(true) ;

 

TableLayout 에서 설정을 해야됩니다. 그 전에 TableRow ,Button에서 해봤지만 안되네요. 

 

화면 소스

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/confButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="2dp"
        android:text="@string/confirm_button"
        tools:context=".GameActivity" />
   
    <TableLayout
        android:id="@+id/table"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:layout_marginEnd="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="1dp"
        android:baselineAligned="true"
        android:stretchColumns="*" >

    </TableLayout>

</RelativeLayout>

------------------------------------------------

 

public class GameActivity extends Activity implements OnClickListener

 {

    private static final int ff0033cc = 0;


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.game);
       
        final TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
        tableLayout.setShrinkAllColumns(true) ;
        
        for (int i = 0; i < 9; i++) {
            // Creation row
            final TableRow tableRow = new TableRow(this);         
           
            for(int j = 0 ; j < 9 ; j++){
                final Button tb = new Button(this);
                tb.setText("d");
                tb.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
               
               
                tableRow.addView(tb);
            }
            tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
           
            tableLayout.addView(tableRow);
        }
       

  
    }


 public void onClick(View arg0) {
  
  // TODO Auto-generated method stub
 
  // String path = getFilesDir().getAbsolutePath();
 
  Intent intent = null;
 

  switch (arg0.getId()) {
 

  }
 

 

 }
 


}

 

-------------------------

결과 화면입니다.

 

 

 

 

 

 

 

반응형