我想要做的是创建ImageView 对象的N个(在本例中为 9 个)副本R.id.tile
,将它们中的每一个放置在布局上的不同坐标处,并为每个对象提供自己的唯一标识符。
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]);
}
...
但是,我在最后一行不断收到“找不到源”layout.addView(tiles[i]);
错误: ..有
什么想法吗?
(附带问题:如果您使用坐标,RelativeLayout 是否比 AbsoluteLayout 更好?)