开发者

Handling Views.IDs & Drawable.Ints in a loop

开发者 https://www.devze.com 2023-02-26 20:31 出处:网络
Well, this is my first topic around here, but that shouldn\'t matter so far. My question is: How can I handle Views with similar IDs (example: R.id.View1, View2, View3, ..) in a loop & how can I

Well, this is my first topic around here, but that shouldn't matter so far.

My question is: How can I handle Views with similar IDs (example: R.id.View1, View2, View3, ..) in a loop & how can I give each of them a different picture out of my drawable resource (example: R.drawable.pic1, pic2, pic3, ..)?

I haven't found an answer on this yet, thought I didn't rly know what to look for in google :P.

To give you a better view about my topic, here is my code:

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="492px"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:id="@+id/puzzletile_r1_c1"
            android:layout_width="82px"
            android:layout_height="82px"
            />
        <ImageView
            android:id="@+id/puzzletile_r1_c2"
            android:layout_width="82px"
            android:layout_height="82px"
            />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:id="@+id/puzzletile_r2_c1"
            android:layout_width="82px"
            android:layou开发者_运维技巧t_height="82px"
            />
        <ImageView
            android:id="@+id/puzzletile_r2_c2"
            android:layout_width="82px"
            android:layout_height="82px"
            />
    </LinearLayout>
</LinearLayout>

There're alot more Linears & ImageViews inside that file, but I deleted them for a better overview.

package fast.games.puzzleme;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

    public class GameActivity extends Activity{

        // Declare variable for checking if the custom Title is supported
        boolean customTitleSupported;

        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);

               setContentView(R.layout.playground);

        }
    }

My general idea was to do sth like this:

for(i = 0; i<= 10; i++){
Imageview iv = (ImageView)this.findViewById(R.id.pic_+i);
iv.setImageResource(R.drawable.img_+i)
}

Thought it doesn't work like that >.<

Is there any way to accomplish that?

Thanks!


You cannot generate view ids like that, but you can findViewByTag() and specify android:tag in playground.xml file. But there's a way to get resource id by name. Just call getResources().getIdentifier("img_" + i, "drawable", getPackageName()) and you'll get img_xxx drawable's id.

0

精彩评论

暂无评论...
验证码 换一张
取 消