简体   繁体   English

继承的RelativeLayout内LinearLayout内ImageView的边距不起作用

[英]Margins of ImageView inside LinearLayout inside inherited RelativeLayout not working

<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 was created to put elements inside, like a RelativeLayout , this way: 创建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;
    }
}

And I am adding some images programmatically inside this searchResultList that is inside HexagonGrid . 我在HexagonGrid内的searchResultListHexagonGrid编程方式添加了一些图像。

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

I am not understanding why setMargins is not working: 我不明白为什么setMargins无法正常工作:

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

Any one can tell me why? 谁能告诉我为什么?

OMG! 我的天啊!

ImageView will have a LinearLayout as its parent, but there is two types of LayoutParams : ImageView将以LinearLayout作为其父级,但是有两种LayoutParams

RelativeLayout.LayoutParams and LinearLayout.LayoutParams RelativeLayout.LayoutParamsLinearLayout.LayoutParams

For some reason, my project automatically selected RelativeLayout.LayoutParams as default. 由于某种原因,我的项目自动将RelativeLayout.LayoutParams选择为默认值。 This way margins don't work. 这样,边距无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM