繁体   English   中英

嵌套的LinearLayouts无法正常工作

[英]Nested LinearLayouts Not Working

我已经尝试了两天了,以创建嵌套的线性布局(线性布局内的线性布局)很少成功。 我的主布局包含3个部分,分别按权重分别为45、45和10。当我运行该布局时,它似乎工作得很好。 我在不同颜色的屏幕上得到3个矩形。

创建“子”线性布局并将其添加到母版后,子布局将主导屏幕。 子线性布局的权重为35,35和30。因此,我希望在屏幕上看到顶部矩形分成3个较细的矩形。 相反,我得到了属于子布局的3个矩形。

有任何想法吗?

公共无效onCreate(捆绑保存的InstanceState){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);

}

您的布局可以正常工作,但是您paramsA为实际添加到masterL LinearLayout的新sub-layout( sub1 )添加LayoutParams paramsA ,而是设置了一组新的LayoutParamswidthheight设置为FILL_PARENT ?!!?)使您的sub1整个主版面。 您所要做的就是将正确的LayoutParams设置为sub1

    sub1.setLayoutParams(paramsA);

注意:正如其他人所说,嵌套权重对于性能而言并不太好,也许您可​​以使用其他类型的布局来改善布局。

仅当将子级的布局参数设置为wrap_content并且它们中确实有额外的空格时,layout weight属性才有用。

首先是在xml中执行此操作,以Java编写时,它很难读取/维护布局代码(特别是当它这么简单时)。 几乎没有充分的理由在Java中编写此类属性。

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

第三,如果您绝对必须使用嵌套权重(同样可以肯定也不要使用嵌套权重),则需要设置sub1的权重。 通过设置其高度以填充父级而不是权重为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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM