Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-将图像从类添加到布局_Android_Layout_Bitmap - Fatal编程技术网

Android-将图像从类添加到布局

Android-将图像从类添加到布局,android,layout,bitmap,Android,Layout,Bitmap,基本理论是这样的:我想创建一个类来保存位图,它是从资源加载的。然后我想为该类创建一个新对象,并将其添加到布局中 到目前为止,除了idk(如何将位图添加到布局)之外,我已经成功地完成了所有工作 我希望它显示在现有的布局上 位图类: 类BitmapView扩展了视图 { 公共位图视图(上下文) { 超级(上下文); } } 我用setContentView(新的位图视图(this))来命名它 但这在整个屏幕上勾勒出了整个画面。我希望它显示在以前的布局中设置的背景。还有什么方法可以设置位图大小吗 谢谢

基本理论是这样的:我想创建一个类来保存位图,它是从资源加载的。然后我想为该类创建一个新对象,并将其添加到布局中

到目前为止,除了idk(如何将位图添加到布局)之外,我已经成功地完成了所有工作

我希望它显示在现有的布局上

位图类:

类BitmapView扩展了视图 { 公共位图视图(上下文) { 超级(上下文); }

}

我用setContentView(新的位图视图(this))来命名它

但这在整个屏幕上勾勒出了整个画面。我希望它显示在以前的布局中设置的背景。还有什么方法可以设置位图大小吗


谢谢

使用
设置图像位图(bmp)
。在布局中放置一个
ImageView
。使用
ImageView im=(ImageView)findviewbyd(R.id.myBitmapId)引用它。创建位图时,只需执行
im.setImageBitmap(bmp)

使用图像视图显示位图

ImageView imgView=new ImageView(context);
imgView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imgView.setBitmapImage(bitmap);   //Your bitmap goes here.
setContentView(imgView);

谢谢,但我想在没有任何xml的情况下通过编程来完成这一切。findViewById不是只获取layout.xml中设置的ID吗?idk我是一个nubhmm好的,我如何传递上下文?顺便说一句,这似乎是我在寻找的。上下文是您的活动实例本身。。。ImageView imgView=新的ImageView(此);行得通
ImageView imgView=new ImageView(context);
imgView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imgView.setBitmapImage(bitmap);   //Your bitmap goes here.
setContentView(imgView);