自定义布局忽略布局参数

时间:2015-10-29 18:35:05

标签: android android-layout

我似乎无法在代码中复制XML等价物:

XML Layout VS Programmed Layout

布局代码

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="3">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_columnSpan="2"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>
    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>
</GridLayout>

Java“等效”

public class ImageSelectionGridView extends GridLayout
{
    private static final int SQUARE_SIZE = 10;

    private int mSquareSize;

    private ImageView mMainImageView;
    private ImageView mRemainingImageViews[];

    public ImageSelectionGridView(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);

        TypedArray a = ctx.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ImageSelectionGridView,
                0, 0);

        try
        {
            mSquareSize = a.getDimensionPixelSize(R.styleable.ImageSelectionGridView_squareSize,
                    SQUARE_SIZE);

        } finally
        {
            a.recycle();
        }

        mMainImageView = new ImageView(ctx);
        mMainImageView.setImageDrawable(ctx.getResources().getDrawable(R.drawable.square_outline));
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = params.height = mSquareSize;

        params.columnSpec = GridLayout.spec(0, 2);
        params.rowSpec = GridLayout.spec(0, 2);
        params.setGravity(Gravity.FILL);
        mMainImageView.setLayoutParams(params);

        addView(mMainImageView);

        mRemainingImageViews = new ImageView[5];
        for (int i = 0; i < mRemainingImageViews.length; i++)
        {
            createRemainingImage(ctx);
        }
    }

    private ImageView createRemainingImage(Context ctx)
    {
        ImageView remainingImage = new ImageView(new ContextThemeWrapper(ctx, R.style.EditablePictureStyle));
        remainingImage.setImageDrawable(ctx.getResources().getDrawable(R.drawable.square_outline));
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = params.height = mSquareSize;

        addView(remainingImage);

        return remainingImage;
    }
}

带有attrs.xml的代码的XML:

<domain.slingbee.ui.ImageSelectionGridView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:columnCount="3"
                        custom:squareSize="70dp"
                        />

<declare-styleable name="ImageSelectionGridView">
        <attr name="squareSize" format="dimension"/>
    </declare-styleable>

由于某种原因,尺寸不同,我将70dp传递给java代码并设置具有特定宽度和高度的所有图像视图,为什么它们不同?

1 个答案:

答案 0 :(得分:0)

createRemainingImage()内,您正在创建layoutParams,但未将其设置为ImageView。

    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
    params.width = params.height = mSquareSize;

您刚刚创建了params。但是你没有将它设置为ImageView。你需要这个:

    remainingImage.setLayoutParams(params);