0

我正在尝试将ImageView屏幕顶部的 aTextView与该图像左下角的 a 对齐。我被难住了,有人知道这是怎么回事吗?我希望它看起来像这样(在图形设计器中看起来不错)

在此处输入图像描述

这是它在应用程序中的样子。请注意,图像和屏幕顶部之间有很大的空间。

在此处输入图像描述

这是xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.package.blah.ImageWithOverlay
        android:id="@+id/imageWithOverlay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.package.blah.ImageWithOverlay>

</RelativeLayout>

这是生成它的代码。

public class ImageWithOverlay extends RelativeLayout {
    ImageView image;
    TextView text;
    int imageHeight;

    public ImageWithOverlay(Context context) {
        super(context);
        setupDisplay(context);
    }

    public ImageWithOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDisplay(context);
    }

    public ImageWithOverlay(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setupDisplay(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, imageHeight);
    }

    private void setupDisplay(Context context) {
        BitmapDrawable bd = (BitmapDrawable)this.getResources().getDrawable(R.drawable.flowers480);
        imageHeight = bd.getBitmap().getHeight();

        image = new ImageView(context);
        image.setImageDrawable(bd);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(ALIGN_PARENT_TOP);
        image.setLayoutParams(lp);
        this.addView(image);

        text = new TextView(context);
        text.setText("testing");
        lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(ALIGN_PARENT_BOTTOM);
        text.setLayoutParams(lp);
        this.addView(text);
    }
}

任何帮助表示赞赏,如果需要,请随时向我询问更多详细信息。

4

1 回答 1

1

我想你会发现 ImageView 的大小是正确的,但是图像在视图内缩放和居中。

要修复,请添加:

image.setAdjustViewBounds(true);

要检查此类问题,您可以尝试:

image.setBackgroundColor(0x6600ffff);

这将使您看到图像的边界。

于 2012-06-22T02:29:37.827 回答