1

我已经尝试了 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

4 回答 4

1

您的布局可以正常工作,但不是LayoutParams paramsA为您实际添加的新子布局(sub1)添加,masterL LinearLayout而是设置了一组新的LayoutParams(使用widthheight设置为FILL_PARENT?!!?),使您sub1填充整个主布局。您所要做的就是将正确设置LayoutParamssub1

    sub1.setLayoutParams(paramsA);

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

于 2012-04-14T07:39:10.177 回答
0

wrap_content布局权重属性仅在子布局参数设置为并且它们确实有额外的空白空间时才有用。

于 2012-04-14T04:30:44.090 回答
0

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

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

第三,如果你绝对必须使用嵌套权重(同样你几乎肯定不会),你需要设置 sub1 的权重。通过将其高度设置为填充父项,而不是使用权重设置为 0,您是在告诉它填充屏幕,因此它完全按照您的指示进行操作也就不足为奇了。

于 2012-04-14T05:40:18.953 回答
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);
于 2012-04-14T05:40:25.723 回答