其他分享
首页 > 其他分享> > android – 如何在布局上为ImageView对象的新副本充气?

android – 如何在布局上为ImageView对象的新副本充气?

作者:互联网

我要做的是创建ImageView对象R.id.tile的N(在本例中为9)副本,将它们中的每一个放置在布局上的不同坐标上,并为每个坐标提供其自己的唯一标识符.

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好吗?)

解决方法:

编辑:

首先使用一个具有您喜欢的属性的ImageView创建布局

例:

在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充气以获得它的副本

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值

tiles[i].setId(i);  

但是你(错误地)试图从你的其他资源(你想要’克隆’)获得它:

layout.addView(tiles[i] = (ImageView) findViewById(R.id.tile)); // INCORRECT

相反,如上所述手动设置id,然后:

layout.addView(tiles[i]);

并且无需调用findViewById()

标签:android,imageview,coordinates,inflate,absolutelayout
来源: https://codeday.me/bug/20191007/1866428.html