如何在Android中动态设置布局参数? [英] How do dynamically set layout parameters in Android?

查看:190
本文介绍了如何在Android中动态设置布局参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我动态生成的这些文本字段。但我似乎无法设置布局参数他们。请告诉我,我做错了。

我能不产生布置参数的字段。不过,如果我使用的LayoutParams,它甚至不会产生。

感谢

code:

  TableLayout表=(TableLayout)findViewById(R.id.TableLayout1);

            的TableRow TR =新的TableRow(本);
            LinearLayout.LayoutParams trparams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            tr.setLayoutParams(trparams);

            CG [i] =新的EditText(本);
            重量[i] =新的EditText(本);
            CG [I] .setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            重量[I] .setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            //这就是它出了问题。通过添加fieldparams,文本字段甚至不会产生。
            LinearLayout.LayoutParams fieldparams =新LinearLayout.LayoutParams(10,LinearLayout.LayoutParams.WRAP_CONTENT,1.0F);
            tr.addView(CG [I],fieldparams);
            tr.addView(重量[I],fieldparams);

            table.addView(TR);
 

解决方案

您添加表行成表,以便使用 TableLayout.LayoutParams 而不是 LinearLayout.LayoutParams 。正如我们使用LayoutParam根据家长中,我们要添加

I have these text fields which I generated dynamically. But I can't seem to set layout parameters for them. Please tell me what I'm doing wrong.

I'm able to generate the fields without layout parameters. However, If I use LayoutParams, it doesn't even get generated.

Thanks

Code:

            TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);

            TableRow tr = new TableRow(this);
            LinearLayout.LayoutParams trparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            tr.setLayoutParams(trparams);

            cg[i] = new EditText(this);
            weight[i] = new EditText(this);
            cg[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            weight[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            //Here's where it goes wrong. By adding fieldparams, the text field doesn't even get generated.
            LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(10, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
            tr.addView(cg[i], fieldparams);
            tr.addView(weight[i], fieldparams);

            table.addView(tr);

解决方案

You are adding Table Row into table so use TableLayout.LayoutParams instead of LinearLayout.LayoutParams.As we use LayoutParam according to parent in which we are going to add

这篇关于如何在Android中动态设置布局参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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