How to inflate new copies of an ImageView object on a layout?(如何在布局上膨胀 ImageView 对象的新副本?)
问题描述
我要做的是创建 ImageView 对象 R.id.tile
的 N(在本例中为 9 个)副本,将它们中的每一个放在不同的坐标在布局上,并为每个布局赋予其唯一标识符.
What I am trying to do is create N (in this case 9) copies of the ImageView object R.id.tile
, place each of them at different coordinates on the layout, and give each its own unique identifier.
board_layout.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"
android:background="@drawable/backgroundcolor"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/topbar"
... >
<ImageButton
android:id="@+id/imagebutton"
... />
</LinearLayout>
<RelativeLayout
android:id="@+id/board"
android:layout_width="match_parent"
android:layout_height="345dp"
android:layout_centerVertical="true"
android:background="@drawable/wwfboard" >
<view class="languid.legend.xsolver.DrawView"
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="@+id/tile"
android:layout_width="21dp"
android:layout_height="21dp"
android:src="@drawable/tile" />
</RelativeLayout>
</RelativeLayout>
BoardLayout.class:
@Override
protected void onCreate(Bundle savedInstance)
{ super.onCreate(savedInstance);
setContentView(R.layout.board_layout);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View[] tiles = new ImageView[9];
entire_layout = (RelativeLayout)findViewById(R.layout.board_layout);
for(int i = 0; i<tiles.length; i++){
tiles[i] = (ImageView) inflater.inflate(R.layout.board_layout, null);
tiles[i].setId(1000+i);
params = new RelativeLayout.LayoutParams(-1, 345);
params.leftMargin = 32*2*i;
params.topMargin = 34*2*i;
entire_layout.addView(tiles[i]);
}
...
但是,我在最后一行不断收到 Source not found" 错误:layout.addView(tiles[i]);
..
任何想法为什么?
(附带问题:如果您使用坐标,RelativeLayout 是否比 AbsoluteLayout 更好?)
However, I keep getting "Source not found" errors on the last line: layout.addView(tiles[i]);
..
Any ideas why?
(Side question: is RelativeLayout better than AbsoluteLayout if you're working with coordinates?)
推荐答案
首先创建一个带有你喜欢的属性的 ImageView 的布局
First create a layout with one ImageView with the properties you like
例子:
在 res/layout/singleimage.xml 中创建文件
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="Sample"/>
然后对 ImageView
进行膨胀以获取它的副本,像这样
And then inflate the ImageView
to get the copies of it like this
View[] tiles = new ImageView[9];
// get reference to LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i<tiles.length; i++) {
//Creating copy of imageview by inflating it
ImageView image = (ImageView) inflater.inflate(R.layout.singleimage, null);
tiles[i] = image;
tiles[i].setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, 345);
params.leftMargin = 32*2*3;
params.topMargin = 34*2*3;
layout.addView(tiles[i]);
}
<小时>
您已在此处将 id
设置为 i
值
tiles[i].setId(i);
但是您(错误地)试图从您的其他资源(您想要克隆"的资源)中获取它:
But you are (incorrectly) trying to get it from your other resource (the one you wanted to 'clone'):
layout.addView(tiles[i] = (ImageView) findViewById(R.id.tile)); // INCORRECT
相反,像上面一样手动设置id
,然后:
Instead, set the id
manually as above, and then:
layout.addView(tiles[i]);
而且不需要调用findViewById()
这篇关于如何在布局上膨胀 ImageView 对象的新副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在布局上膨胀 ImageView 对象的新副本?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01