【问题标题】:Custom Layout ignoring layout params自定义布局忽略布局参数
【发布时间】:2015-10-30 02:44:22
【问题描述】:

我似乎无法在代码中复制等效的 XML:

布局代码

<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 代码并设置所有具有特定宽度和高度的图像视图,为什么它们不同?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在您的 createRemainingImage() 中,您正在创建 layoutParams,但没有将它们设置为 ImageView。

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

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

        remainingImage.setLayoutParams(params);
    

    【讨论】:

    • 谢谢,多一双眼睛总是有用的。
    猜你喜欢
    • 2014-03-09
    • 2017-09-05
    • 1970-01-01
    • 2023-04-07
    • 2020-09-24
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多