2015-11-20 55 views
7

當由XML添加按鈕不正確的 - 所有好對齊當添加視圖動態地GridLayout的

When adding buttons through XML

<GridLayout 
    android:id="@+id/social_gl_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:alignmentMode="alignBounds" 
    android:columnCount="2" 
    android:padding="8dp"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="45dp" 
     android:layout_columnWeight="1"> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="45dp" 
      android:background="@android:color/holo_blue_light" 
      android:text="Hi"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="45dp" 
     android:layout_columnWeight="1"> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="45dp" 
      android:background="@android:color/holo_green_light" 
      android:text="Whatsapp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="45dp" 
     android:layout_columnWeight="1"> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="45dp" 
      android:background="@android:color/holo_green_light" 
      android:text="This is facebook" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="45dp" 
     android:layout_columnWeight="1"> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="45dp" 
      android:background="@android:color/holo_blue_light" 
      android:text="On" 
      /> 
    </LinearLayout> 

</GridLayout> 

當添加按鈕動態地(通過代碼) - 對齊丟失,不佔用列的完整寬度的按鈕

GridLayout gl = (GridLayout) findViewById(R.id.social_gl_content); 
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    for (int i = 0 ; i < strs.length ; i++) { 
     View v = inflater.inflate(R.layout.grid_item, null); 
     Button b = (Button) v.findViewById(R.id.button); 
     b.setText(strs[i]); 
     if (i % 2 ==0) { 
      b.setBackgroundColor(Color.BLACK); 
     }else{ 
      b.setBackgroundColor(Color.BLUE); 
     } 
     gl.addView(v); 
    } 

enter image description here

+0

看看這裏.. http://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly – Sjd

+0

試試這個LinearLayout.LayoutParams PARMS =新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); b.setLayoutParams(parms); –

回答

5

強似空的:

View v = inflater.inflate(R.layout.grid_item, null);

通過parentView使V有正確的佈局PARAMS。

View v = inflater.inflate(R.layout.grid_item, gl, false);

+0

https://possiblemobile.com/2013/05/layout-inflation-as-intended/ –

+0

我重複了原來的問題,然後我嘗試了這個解決方案,並以空白屏幕結束。 – Tequilaman

1

試試這個:

LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
b.setLayoutParams(parms); 
+0

什麼都沒有發生。 –