[英]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
,而是設置了一組新的LayoutParams
( width
和height
設置為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.