Program ImageView [] [] ID - GridLayout

时间:2017-01-19 13:32:28

标签: android imageview android-gridlayout

我使用这些代码在GridLayout上制作 2x2图像并且它有效,但有一个问题,每个ImageView具有相同的ID,{{1} },所以当我使用-1时,它不起作用。我该怎么办?

这里是代码

activity_main.xml:

OnClick

MainActivity.java:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"      
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridLayout"
    tools:context=".MainActivity"
    android:orientation="horizontal" 
    android:columnCount="2"
    android:rowCount="2" />

1 个答案:

答案 0 :(得分:0)

您的视图ID只需要是正整数,因此可以像下面这样设置它们:

fileChange(event) {
    let files = event.target.files;
        if (files.length > 0) {
        let formData: FormData = new FormData();
        for (let file of files) {
             formData.append('files', file, file.name);
        }
        let headers = new Headers();
        headers.set('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        this.http.post(uploadURL, formData, options)
            .map(res => res.json())
            .catch(error => Observable.throw(error))
            .subscribe(
            data => {
                // Consume Files
                // ..
                console.log('uploaded and processed files');
            },
            error => console.log(error),
            () => {
                this.sleep(1000).then(() =>
                    // .. Post Upload Delayed Action
                )
            });
    }
}

或者您可以设置点击监听程序:

imageViews[i][j].setId(100);   // or whatever you prefer