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