【问题标题】:Nested LinearLayouts Not Working嵌套的线性布局不起作用
【发布时间】:2012-04-14 15:39:10
【问题描述】:

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

}

【问题讨论】:

  • TestView 是您自定义的线性布局吗?
  • 请分享一些描述您的目标布局的图片。
  • 是否有必要在运行时生成,你也可以制作XML,这样会容易得多。
  • 它终于开始工作了 - 感谢您的帮助。

标签: android android-linearlayout


【解决方案1】:

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

    sub1.setLayoutParams(paramsA);

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

【讨论】:

  • 非常感谢 - 现在正在运行。一旦我阅读了您的评论,就很有意义。不知道为什么我以前无法掌握这一点。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      你需要:

      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);
      

      【讨论】:

      • 另外,使用 xml 会更好。
      • @Jack:如果它适合你,你可以投票并接受答案。
      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多