如何以编程方式在linearlayout android中添加复选框旁边的复选框?

时间:2015-08-11 19:41:45

标签: android android-layout

我有一个线性布局,我想在左边插入一个复选框,在右边插入一个按钮。 此外,它应该在for循环中,以便在单个线性布局中创建许多这样的组合。

    final LinearLayout LayoutRight = (LinearLayout) findViewById(R.id.linearlayoutbars_register);
    LayoutRight.setOrientation(LinearLayout.VERTICAL);
    LayoutRight.setWeightSum(1f);

            final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
            pR1.weight = 0.3f;

            final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
            pR2.weight = 0.7f;


              for (int i = l; i <= lastCounter; i++) {

                tick = new CheckBox(RegisterActivity.this);
                tick.setId(10000 + l);
                tick.setOnClickListener(tick_Click);
                LayoutRight.addView(tick, pR1);

                pdR = new Button(RegisterActivity.this);
                pdR.setText(l + ". ");
                pdR.setId(l);
                pdR.setWidth(800);
                pdR.setTextSize(17);
                pdR.setGravity(Gravity.START);
                pdR.setOnClickListener(pdRclass);
                LayoutRight.addView(pdR, pR2);

                l++;
            }

我将1f设置为setWeightSum; 0.3f和0.7f和孩子的体重。 使用此代码,我将输出为this 但我希望我的输出完全像this

2 个答案:

答案 0 :(得分:0)

你需要设置布局方向像这样 LayoutRight.setOrientation(LinearLayout.HORIZONTAL); 不是垂直的

答案 1 :(得分:0)

LayoutRight.setOrientation(LinearLayout.HORIZONTAL)?

我不够酷,所以评论......

Android weight doc表示受影响的属性需要设置为0dp,所以试试这个。

        final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
        0,
        LinearLayout.LayoutParams.WRAP_CONTENT);
        pR1.weight = 0.3f;

        final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
        0,
        LinearLayout.LayoutParams.WRAP_CONTENT);
        pR2.weight = 0.7f;

不要忘记让你的方向保持水平!