代码创建线性布局(没有 XML)并为元素设置权重

Code creation linearlayout (without XML) and setting weight to elements

所以,我的想法是在 .java 文件中实现带有 MatchParent(width) 的 lLayout,因为它是动态创建的。Expectation and Real

代码:

//lLayout block
LinearLayout block = new LinearLayout(this);
block.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams blockParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.addView(block, blockParams);

//lLayout lText
LinearLayout lText = new LinearLayout(this);
lText.setBackgroundColor(Color.argb(69,0,0,0));
lText.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.67f);
lTextParams.setMargins(10, 10, 0, 10);
lText.setPadding(10, 10, 10, 10);
block.addView(lText, lTextParams);

//TextView new_aim
//LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView new_aim = new TextView(this);
new_aim.setText(aim);
new_aim.setTextSize(20);
lText.addView(new_aim, blockParams);

//TextView new_dscrp
LinearLayout.LayoutParams lllParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lllParams.setMargins(20, 0, 0, 0);
TextView new_dscrp = new TextView(this);
new_dscrp.setText(description);
new_dscrp.setTextSize(15);
lText.addView(new_dscrp, lllParams);

//Delete TextView
TextView b_del = new TextView(this);
b_del.setText("Delete!");
b_del.setBackgroundColor(Color.argb(69, 0, 0, 0));
b_del.setTextSize(15);
LinearLayout.LayoutParams del_Params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 0.33f);
del_Params.setMargins(0, 10, 10, 10);
b_del.setPadding(10, 10, 10, 10);
block.addView(b_del, del_Params);

我尝试了任何只能在网上找到的东西,但没有任何结果。请帮我解决这个问题。

提前谢谢你)

        //lLayout block
        LinearLayout block = new LinearLayout(this);
        block.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams blockParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lTextParams.weight = 1;
        llMain.addView(block, blockParams);

        //lLayout lText
        LinearLayout lText = new LinearLayout(this);
        lText.setBackgroundColor(Color.argb(69,0,0,0));
        lText.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams lTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.67f);
        lTextParams.setMargins(10, 10, 0, 10);
        lTextParams.weight = 1;
        lText.setPadding(10, 10, 10, 10);
        block.addView(lText, lTextParams);

        //TextView new_aim
        //LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView new_aim = new TextView(this);
        new_aim.setText(aim);
        new_aim.setTextSize(20);
        lText.addView(new_aim, blockParams);

        //TextView new_dscrp
        LinearLayout.LayoutParams lllParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lllParams.setMargins(20, 0, 0, 0);
        lllParams.weight =1;
        TextView new_dscrp = new TextView(this);
        new_dscrp.setText(description);
        new_dscrp.setTextSize(15);
        lText.addView(new_dscrp, lllParams);

        //Delete TextView
        TextView b_del = new TextView(this);
        b_del.setText("Delete!");
        b_del.setBackgroundColor(Color.argb(69, 0, 0, 0));
        b_del.setTextSize(15);
        LinearLayout.LayoutParams del_Params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 0.33f);
        del_Params.setMargins(0, 10, 10, 10);
        del_Params.weight =1;
        b_del.setPadding(10, 10, 10, 10);
        block.addView(b_del, del_Params);