Android 입문 TableLayout 응용 분석(2)

2401 단어 AndroidTableLayout
본 고 는 지난 편 에서 TableLayout 의 상용 속성 을 초보 적 으로 소개 한 토대 에서 UI 디자이너 가 TableLayout+TableRow 를 디자인 하 는 방법 을 소개 할 것 이다.실제 응용 에서 코드 에 TableLayout 에 데 이 터 를 추가 해 야 하기 때문에 본 고 는 이 방면 의 간단 한 사용 방법 을 소개 하고 자 한다.
main.xml 코드 는 다음 과 같 습 니 다.TableLayout 의 ID 는 TableLayout 01 입 니 다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
   <TableLayout 
       android:id="@+id/TableLayout01" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content">
   </TableLayout>
</LinearLayout>

JAVA 코드 부분 은 다음 과 같 습 니 다.

package com.LayoutDemo;
import com.LayoutDemo.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class LayoutDemo extends Activity {
  /** Called when the activity is first created. */
 private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
 private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //  TableLayout01   
    TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
    //          
    tableLayout.setStretchAllColumns(true);
    //  10 ,8    
    for(int row=0;row<10;row++)
    {
      TableRow tableRow=new TableRow(this);
      for(int col=0;col<8;col++)
      {
       //tv    
       TextView tv=new TextView(this);
        tv.setText("("+col+","+row+")");
        tableRow.addView(tv);
      }
      //   TableRow   TableLayout
      tableLayout.addView(tableRow, new TableLayout.LayoutParams(FP, WC));
    }
  }
}

결 과 는 다음 그림 과 같다.

좋은 웹페이지 즐겨찾기