如何以编程方式将表格布局高度平均分配给表格行 [英] How to equally divide the table layout height to table rows in programmatically

查看:100
本文介绍了如何以编程方式将表格布局高度平均分配给表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是android开发的初学者。在我的应用程序中,我动态创建了tablerow。我想平均将表格布局高度划分为表格行。但它没有用......



我创建9行每行包含9个TextView



我的xml是



Hi,

I am beginner in android development. In my application I was creating tablerow dynamically. I want to equally divide the table layout height to table rows. But its not working...

I am creating 9 rows each row contains 9 TextView

My xml is

<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:removed="#000"

        android:paddingTop="1dp"

        android:paddingLeft="1dp"

        android:paddingRight="1dp"

        android:paddingBottom="1dp"

        android:id="@+id/sudokuboard"

        android:stretchColumns="*"

        android:weightSum="100" >

    </TableLayout> 

< br $> b $ b

我的代码是





And my code is

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

   }

推荐答案





我有完全相同的问题。 :D



似乎在代码中将行添加到TableLayout时,不考虑TableRow中的weight参数。



在静态XML布局文件中它可以工作。





更新1

我找到了这里的解决方案!



关键句是一个小部件必须有它的

*父*的LayoutParams。
,所以你必须为你的表行更改 TableRow.LayoutParams TableLayout.LayoutParams

希望它有效!
Hi,

I have the exact same problem. :D

It seems, that the weight parameter in a TableRow is just not considered when adding the row to a TableLayout in code.

In a static XML-layout-file it works.


Update 1
I found the solution here!

The key sentence is "A widget must have the LayoutParams of its
*parent*."
, so you have to change the TableRow.LayoutParams to TableLayout.LayoutParams for your table rows.
Hope it works!


这篇关于如何以编程方式将表格布局高度平均分配给表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆