1

我正在开发一个二十一点 Android 应用程序,我正在使用这种方法来膨胀卡片图像。我使用卡片的值来确定要加载什么图像,并使用索引来知道要将它添加到哪个 ImageView。对于玩家手中的每张牌,我都会这样称呼。第一张卡显示,但之后没有显示。我检查了日志,第二张卡片的图像名称和位置是正确的。

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>

播放器布局xml

<LinearLayout
android:id="@+id/playerhandlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
4

0 回答 0