自定义视图,高度和宽度与背景可绘制相同

时间:2014-08-26 12:45:54

标签: android views android-custom-view

正在开发一个Android应用程序,我有一个自定义视图(扩展Surfaceview)..现在面临一个问题,视图总是将其高度和宽度设置为match_parent ..

我已将其更改为Xml布局文件中的wrap_content并且没有任何更改,即使在图形布局(android xml ui的设计视图)中也在全屏幕中,并且在运行时也是如此。

我需要的是,我正在使用图库中的图像动态更改背景。 像这样,

Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageUri);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BitmapDrawable background = new BitmapDrawable(getResources(), bitmap);

        myDrawableView.setBackgroundDrawable(background);

我希望视图能够使背景图像适合屏幕并且视图将包裹此背景图像,因此它的大小将与该背景图像的大小相同

任何帮助将不胜感激 谢谢

1 个答案:

答案 0 :(得分:0)

您需要覆盖onMeasure()方法,查看SDK目录中的http://developer.android.com/guide/topics/ui/custom-components.html#custom和LabelView示例,了解如何操作。

相关问题