2015-03-08 81 views
0
<br.application.component.HexagonGrid 
    android:id="@+id/hexagonGrid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/searchResultList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="12dp" 
     android:orientation="vertical" > 
    </LinearLayout> 
</br.application.component.HexagonGrid> 

HexagonGrid是爲了把元素中,像RelativeLayout,這種方式:邊距繼承的RelativeLayout內不工作

public class HexagonGrid extends RelativeLayout { 
    public HexagonGrid(Context context) { 
     super(context); 
    } 

    public HexagonGrid(Context context, AttributeSet aSet) { 
     super(context, aSet); 
    } 

    [...] 

    private ImageView createGrayHexagonDefault() { 
     ImageView grayHexagon = new ImageView(context); 
     LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     lp.setMargins(10, 0, 10, 0); 
     grayHexagon.setScaleType(ScaleType.MATRIX); 
     grayHexagon.setImageDrawable(getResources().getDrawable(R.drawable.hexagon_disabled)); 
     grayHexagon.setLayoutParams(lp); 
     return grayHexagon; 
    } 
} 

而且我編程方式添加此searchResultList這是裏面的一些圖片在HexagonGrid內。

LinearLayout searchResultList= (LinearLayout) findViewById(R.id.searchResultList); 
searchResultList.addView(createGrayHexagonDefault()); 

我不理解爲什麼setMargins不工作:

lp.setMargins(10, 0, 10, 0); 

任何一個能告訴我爲什麼嗎?

+0

我會嘗試在開發人員選項中啓用「顯示佈局邊界」以查看是否有邊距設置。 – traninho 2015-03-08 19:40:56

+0

我試過了,但是'ImageView'的邊距沒有出現。 @traninho,請幫助我! – Richard 2015-03-08 19:43:49

+0

你可以試着不要調用grayHexagon.setLayoutParams(lp);和searchResultList.addView(grayHexagon);而不是這個調用searchResultList.addView(grayHexagon,lp); ? – traninho 2015-03-08 19:49:59

回答

0

OMG!

ImageView將有LinearLayout作爲其母公司,但有兩種類型的LayoutParams

RelativeLayout.LayoutParamsLinearLayout.LayoutParams

出於某種原因,我的項目自動選擇RelativeLayout.LayoutParams爲默認。這種方式利潤率不起作用。