wis*_*ndy 7 android subview imageview addsubview
我来自iOS背景.出于某种原因,我无法弄清楚如何将视图添加到另一个视图.
我有两个ImageView以编程方式创建的s,如下所示:
ImageView imageView;
ImageView imageHolder;
Run Code Online (Sandbox Code Playgroud)
现在,我想做这样的事情:
imageHolder.addView(imageView);
Run Code Online (Sandbox Code Playgroud)
我该如何做到这一点?做了很多谷歌搜索,但没有用.
Ada*_*m S 10
正如pskink所说,您只能以编程方式将视图添加到ViewGroup.您可以添加到a LinearLayout,例如:
LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);
layout.addView(new EditText(context));
Run Code Online (Sandbox Code Playgroud)
不过,这可能对你的场景没有帮助.要将图像放在另一个图像上,可以使用相对布局.您通常会在XML布局文件中进行设置:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/foregroundImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/backgroundImage"
android:layout_alignLeft="@id/backgroundImage" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
然后,如果您事先不知道它们将会是什么,您可以在代码中指定图像:
((ImageView)findViewById(R.id.backgroundImage)).setImageResource(R.drawable.someBackgroundImage);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11019 次 |
| 最近记录: |