iteye_4268
2011-12-08 11:19
浏览 563
已采纳

android 纯代码TableLayout布局

谁可以帮忙写个纯代码的表格布局示例:

样式 :

     top

left center right

   bottom
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • vishare 2011-12-12 15:18
    最佳回答

    main.xml
    [code="java"]<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mytable"
        android:layout_width="260px"
        android:layout_height="260px"
        android:stretchColumns="*"
        android:layout_span="3"
        ></TableLayout>
    

    [/code]
    主要是加了这部分

    java部分
    [code="java"]
    package com.androidtest.drawtablelayout;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TableLayout;
    import android.widget.TableLayout.LayoutParams;
    import android.widget.TableRow;
    import android.widget.TextView;

    public class DrawTableLayoutByJavaActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TableLayout mytable = (TableLayout) findViewById(R.id.mytable);
    int numberOfRow =3;
    int numberOfColumn =3;
    int cellDimension =24;
    int cellPadding =2;
    for (int row = 0;row<numberOfRow; row ++){
    TableRow tableRow = new TableRow(this);
    tableRow.setLayoutParams(new LayoutParams((cellDimension+2*cellPadding)*numberOfColumn,cellDimension +2*cellPadding));
    for (int column =0;column<numberOfColumn; column++){
    TextView textView = new TextView(this);
    textView.setText("");
    if(row == 0 && column == 1) textView.setText("top");
    if(row == 1 && column == 0) textView.setText("left");
    if(row == 1 && column == 1) textView.setText("center");
    if(row == 1 && column == 2) textView.setText("right");
    if(row == 2 && column == 1) textView.setText("button");
    tableRow.addView(textView);
    }
    mytable.addView(tableRow,new LayoutParams((cellDimension+2*cellPadding)*numberOfColumn,cellDimension +2*cellPadding));
    }
    }
    }
    [/code]

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题