如何在Android中动态设置布局参数?布局、参数、动态、如何在

2023-09-05 02:31:09 作者:清风挽去

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

我能不产生布置参数的字段。不过,如果我使用的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根据家长中,我们要添加

Android Studio 如何修改工程的默认布局文件

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

 
精彩推荐
图片推荐