在android应用程序中将一系列图像膨胀为linearlayout

时间:2013-10-30 02:02:24

标签: android android-imageview layout-inflater blackjack

我正在使用二十一点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>

playerhandlayout xml

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

0 个答案:

没有答案