如何以编程方式创建宽度相等的两个LinearLayout?

时间:2016-02-18 10:00:46

标签: android android-linearlayout android-layoutparams

我正在尝试以编程方式创建两个宽度相等的LinearLayoutenter image description here

mGroupLayout.setOrientation(HORIZONTAL);
mGroupLayout.setWeightSum(2f);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        0,
        LinearLayout.LayoutParams.WRAP_CONTENT
        );
params.weight = 1.0f;


leftLayout = new LinearLayout(getContext());
leftLayout.setOrientation(VERTICAL);
leftLayout.setLayoutParams(params);


mGroupLayout.addView(
        leftLayout,
        params
        );

rightLayout = new LinearLayout(getContext());
rightLayout.setOrientation(VERTICAL);
rightLayout.setLayoutParams(params);

mGroupLayout.addView(
        rightLayout,
        params
        );

但我的所有线性布局都不可见(它们的宽度为0)。 我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        0,
        LinearLayout.LayoutParams.MATCH_PARENT,1
        );
FirstLinearLayout.setLayoutParams(params );
SecondLinearLayout.setLayoutParams(params );

1 is the Weight of each layout.

答案 1 :(得分:1)

试试这个,

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.MATCH_PARENT,1.0
            );

1.0如果 weightum 是2,则为其他0.5