【问题标题】:Android: How to equally divide the table layout height to table rows in programmaticallyAndroid:如何以编程方式将表格布局高度平均划分为表格行
【发布时间】:2014-01-03 17:49:17
【问题描述】:

我正在动态创建 tablerow(9 行)。我想将表格布局高度平均划分为表格行。但它不起作用......

我正在创建 9 行,每行包含 9 个 TextView

我的xml是

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/header"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"                                 
            android:orientation="vertical"  
            android:weightSum="100"   >

    <TableLayout 
        android:layout_weight="80"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#000"
        android:paddingTop="1dp"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingBottom="1dp"
        android:id="@+id/sudokuboard"
        android:stretchColumns="*"
        android:weightSum="100" >

    </TableLayout>

我的代码是

tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);

    //1st Row

    for(int i=0; i < 9; i++)
    {
        TableRow NewRow1 =new TableRow(this);
        NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,10.0f));

        NewRow1.setPadding(1, 1, 1, 1);

        for(int j=0; j<9; j++)
        {
            TextView tv_00 = new TextView(this);
            int id=10*i;
            id=id+j;
            String strText=""+id;
            tv_00.setText(strText);
            tv_00.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f));
            tv_00.setGravity(Gravity.CENTER);
            tv_00.setBackgroundColor(Color.parseColor("#FFFFFF"));
            tv_00.setId(id);
            NewRow1.addView(tv_00);
        }
        //tblsudoku.addView(NewRow1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tblsudoku.addView(NewRow1);

    }

请帮我解决这个问题...

2013 年 12 月 29 日更新

新代码

        tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
    for(int i=0; i < 9; i++)
    {
        TableRow NewRow1 =new TableRow(this);
        NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,1f));
        NewRow1.setBackgroundColor(Color.RED);

        LinearLayout Newrowlayout =new LinearLayout(this);
        Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
        Newrowlayout.setOrientation(1);

        NewRow1.setPadding(1, 1, 1, 1);

        for(int j=0; j<9; j++)
        {
            TextView tv_00 = new TextView(this);
            int id=10*i;
            id=id+j;
            String strText=""+id;
            tv_00.setText(strText);
            tv_00.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT, 1f));
            tv_00.setGravity(Gravity.CENTER);
            tv_00.setTextColor(Color.YELLOW);
            tv_00.setId(id);
            Newrowlayout.addView(tv_00);
        }
        NewRow1.addView(Newrowlayout);
        tblsudoku.addView(NewRow1);

    }

设计

 <TableLayout   xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"                    
            android:background="#000"
            android:paddingTop="1dp"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingBottom="1dp"
            android:id="@+id/sudokuboard">
         </TableLayout>

【问题讨论】:

    标签: java android


    【解决方案1】:

    关键句是“一个小部件必须有它的 LayoutParams parent.",因此您必须将表格行的 TableRow.LayoutParams 更改为 TableLayout.LayoutParams。 希望它有效!

    更多详情点击Here

    【讨论】:

      【解决方案2】:

      下次请在发布问题之前使用右上角的搜索栏。看看下面的帖子,我相信这就是你要找的。​​p>

      Table Layout with equal rows height

      【讨论】:

      • 感谢您的 cmets。我检查了链接,但我想动态创建一个表。我是android初学者,请帮忙。我还用最新的代码和 XML 更新了问题。
      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2022-01-08
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多