我有一个相对布局:
RelativeLayout battle_layout = (RelativeLayout) findViewById(R.id.battle);
我需要在此处附加一个子项(来自 resources/drawable 的 JPG 图像)。
我之前是用 C# 和 javascript 编写的,但这里实现起来有点困难:)
有人可以帮助我吗?谢谢!
最佳答案
也许您可以使用这些选项之一。以编程方式或在 xml 中:
- 将 ImageView 添加到布局中
- 将图像设置为布局的背景
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/YOUR_IMAGE">
<ImageView
android:src="@drawable/YOUR_IMAGE"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
代码
ImageView imageView = new ImageView(this);
image.setImageResource(R.drawable.YOUR_IMAGE);
battle_layout.addView(image);
关于Java - 如何将 JPG/PNG 文件从资源(可绘制)添加到布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31542892/