私はブラックジャック Android アプリに取り組んでおり、この方法を使用してカード画像を膨らませています。カードの値を使用してロードする画像を決定し、インデックスを使用して追加する ImageView を特定します。プレイヤーの手札のカードごとにこれを呼び出します。最初のカードが表示されますが、その後は表示されません。ログを確認したところ、2 枚目のカードの画像の名前と場所は正しいです。
for(int i = 0; i < playerHand.getCardCount(); i++)
{
addPlayerCardImage(playerHand.getCardByIndex(i), i);
}
private void addPlayerCardImage(Card pCard, int cardIndex)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cardImageView = inflater.inflate(R.layout.cardlayout, playerhandlayout , false);
String selectedImage = "card" + pCard.getValue() + pCard.getSuitAsCharacter();
int CardImageID = getResources().getIdentifier(selectedImage, "drawable",getPackageName());
Log.d("FLAG", "Card " + selectedImage);
String cardLocation = "imageViewCard" + cardIndex;
int cardLocationID = getResources().getIdentifier(cardLocation, "id",getPackageName());
Log.d("FLAG", "Card location " + cardLocation);
ImageView cardImage = (ImageView) cardImageView.findViewById(cardLocationID);
cardImage.setImageResource(CardImageID);
playerhandlayout.addView(cardImageView);
}
これは膨張したレイアウト XML です。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageViewCard0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/cardback" />
<ImageView
android:id="@+id/imageViewCard1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/cardback" />
<ImageView
android:id="@+id/imageViewCard2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/cardback" />
<ImageView
android:id="@+id/imageViewCard3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/cardback" />
<ImageView
android:id="@+id/imageViewCard4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/cardback" />
</LinearLayout>
playerhandlayout xml
<LinearLayout
android:id="@+id/playerhandlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>