嵌套的LinearLayouts不起作用

时间:2012-04-14 03:27:35

标签: android android-linearlayout

我现在已经尝试了2天来创建嵌套线性布局(线性布局内的线性布局),但收效甚微。我的主布局有3个部分,加权45,45& 10.当我运行它时,它似乎工作得很好。我在屏幕上有3个不同颜色的矩形。

一旦我创建了“子”线性布局并将其添加到母版,子布局就占据了屏幕的主导地位。子线性布局的权重为35,35& 30.所以我希望在屏幕上看到顶部矩形分成3个较薄的矩形。相反,我得到了属于子布局的3个矩形。

有什么想法吗?

public void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);

    // Ensure there is a full screen blank window to work with
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

      testViewA = new TestView(this);
      testViewB = new TestView(this);
      testViewC = new TestView(this);

      testViewD = new TestView(this);
      testViewE = new TestView(this);
      testViewF = new TestView(this);

      testViewA.color = 0;
      testViewB.color = 1;
      testViewC.color = 2;
      testViewD.color = 3;
      testViewE.color = 4;
      testViewF.color = 5;

    LinearLayout.LayoutParams paramsA = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .45f);
    LinearLayout.LayoutParams paramsB = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .45f);
    LinearLayout.LayoutParams paramsC = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .10f);

    LinearLayout.LayoutParams paramsX = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .35f);
    LinearLayout.LayoutParams paramsY = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .35f);
    LinearLayout.LayoutParams paramsZ = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .30f);

    paramsA.setMargins(10, 10, 10, 10);
    paramsB.setMargins(10, 10, 10, 10);

    testViewA.setLayoutParams(paramsA);
    testViewB.setLayoutParams(paramsB);
    testViewC.setLayoutParams(paramsC);
    testViewD.setLayoutParams(paramsX);
    testViewE.setLayoutParams(paramsY);
    testViewF.setLayoutParams(paramsZ);

    LinearLayout sub1 = new LinearLayout(this);
    sub1.setOrientation(LinearLayout.VERTICAL);
    sub1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    sub1.addView(testViewD);
    sub1.addView(testViewE);
    sub1.addView(testViewF);

    LinearLayout masterL = new LinearLayout(this);
    masterL.setOrientation(LinearLayout.VERTICAL);
    masterL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    masterL.addView(sub1);
    masterL.addView(testViewB);
    masterL.addView(testViewC);

    setContentView(masterL);

}

4 个答案:

答案 0 :(得分:1)

您的布局可以正常运行,但不是为您实际添加到LayoutParams {{的新子布局(paramsA)添加sub1 masterL。 1}}您设置了一组新LinearLayoutLayoutParamswidth设为height?!!?),使您FILL_PARENT填满整个主布局。您所要做的就是将正确的sub1设置为LayoutParams

sub1

注意: 正如其他所说的嵌套重量对性能不是很好,也许你可以用其他类型的布局改进你的布局。

答案 1 :(得分:0)

布局权重属性仅在子项的布局参数设置为wrap_content并且它们内部有额外的空格时才有用。

答案 2 :(得分:0)

第一件事是在xml中这样做,它很难读取/维护布局代码(特别是当它这么简单时)用Java编写。很少有理由在Java中用这些属性编写。

其次,不要嵌套权重,这对性能不利:http://developer.android.com/resources/articles/layout-tricks-efficiency.html 您应该能够提出一种不需要嵌套布局的替代布局。

第三,如果你绝对必须使用嵌套权重(你几乎肯定不会),你需要设置sub1的权重。通过将其高度设置为填充父级,而不是使用权重为0,您可以告诉它填充屏幕,因此它完全按照您所说的那样做并不奇怪。

答案 3 :(得分:0)

你需要:

1)将孩子的身高设置为0,为你设定体重

2)设置父布局的setweightSum(子项的权重之和)。

检查此代码作为我从您的代码示例中创建的示例:

 requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

          TextView TextViewA = new TextView(this);
          TextView   TextViewB = new TextView(this);
          TextView      TextViewC = new TextView(this);

          TextView      TextViewD = new TextView(this);
          TextView      TextViewE = new TextView(this);
          TextView      TextViewF = new TextView(this);

         TextViewA.setBackgroundColor( Color.RED);
          TextViewB.setBackgroundColor( Color.BLACK);
          TextViewC.setBackgroundColor( Color.BLUE);
          TextViewD.setBackgroundColor( Color.CYAN);
          TextViewE.setBackgroundColor( Color.GRAY);
          TextViewF.setBackgroundColor( Color.GREEN);



       LinearLayout.LayoutParams paramsA = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, .45f);
        LinearLayout.LayoutParams paramsB = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,0, .45f);
        LinearLayout.LayoutParams paramsC = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,0, .10f);

        LinearLayout.LayoutParams paramsX = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, 0,.35f);
        LinearLayout.LayoutParams paramsY = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,0, .35f);
        LinearLayout.LayoutParams paramsZ = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,0, .30f);

        paramsA.setMargins(10, 10, 10, 10);
       paramsB.setMargins(10, 10, 10, 10);

      TextViewA.setLayoutParams(paramsA);
        TextViewB.setLayoutParams(paramsB);
        TextViewC.setLayoutParams(paramsC);
        TextViewD.setLayoutParams(paramsX);
        TextViewE.setLayoutParams(paramsY);
        TextViewF.setLayoutParams(paramsZ);

        LinearLayout sub1 = new LinearLayout(this);
        sub1.setOrientation(LinearLayout.VERTICAL);

        sub1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,0,0.45f));
        sub1.setWeightSum(1f);
        sub1.addView(TextViewD);
        sub1.addView(TextViewE);
        sub1.addView(TextViewF);

        LinearLayout masterL = new LinearLayout(this);
        masterL.setOrientation(LinearLayout.VERTICAL);
        masterL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
       masterL.setWeightSum(1f);
        masterL.addView(sub1);
        masterL.addView(TextViewB);
        masterL.addView(TextViewC);

        setContentView(masterL);