在继承的RelativeLayout里面的LinearLayout里面的ImageView边距不起作用

时间:2015-03-08 18:44:24

标签: android android-layout inheritance margins

<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一样:

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

任何人都可以告诉我为什么?

1 个答案:

答案 0 :(得分:0)

OMG!

ImageView将以LinearLayout作为其父级,但有两种类型的LayoutParams

RelativeLayout.LayoutParamsLinearLayout.LayoutParams

出于某种原因,我的项目自动选择了RelativeLayout.LayoutParams作为默认值。这种边距不起作用。