2012-04-06 140 views
2

我和我的同事正在嘗試開發針對android的紙牌遊戲。 某玩家的牌顯示在屏幕的底部,而他的對手的卡顯示在屏幕的右上方和左上方的邊緣:Android - 在運行時更改ImageView位置

http://i39.tinypic.com/i1lhsm.jpg

我們會很非常喜歡添加以下功能: 當用戶點擊其中一張他的卡片(OnClickListener)時,他選擇的卡片會在玩家其他卡片上方增加幾個像素。 如果用戶選擇另一張卡片,則前一個卡片返回到其初始位置(在屏幕的底部),並且新卡片上升。

整個屏幕被表示在一個RelativeLayout的:它正顯示在屏幕的底部

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/game_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#196DBB"> 


    <RelativeLayout android:id="@+id/battle_field" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/left_name" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:textColor="#FFFFFF" /> 

    <RelativeLayout android:id="@+id/left_cards" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/left_name"> 
    </RelativeLayout> 
    <TextView 
     android:id="@+id/my_name" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:textColor="#FFFFFF" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="40dp" 
     android:layout_centerHorizontal="true"/> 


    <TextView 
     android:id="@+id/right_name" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:textColor="#FFFFFF" /> 

    <RelativeLayout android:id="@+id/right_cards" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/right_name" 
     android:layout_alignParentRight="true"> 
    </RelativeLayout> 

</RelativeLayout> 

該卡,是在運行時添加的,的onCreate期間:

fillLinearLayout(parseAndFillArray(myCardsArray), R.id.game_layout); 

的實施fillLinearLayout如下:

private void fillLinearLayout(LinkedList<Integer> cards, int linearID){ 
     int myCardsNum = cards.size(); 
     int cardWidth = (linearID == R.id.game_layout)? cardWidthHorizontal:cardWidthVertical; 
     RelativeLayout myCardsLayout = (RelativeLayout) findViewById(linearID); 
     if (linearID != R.id.game_layout) myCardsLayout.removeAllViews(); 
     for (int i = 0 ; i < myCardsNum ; i++){ 
      ImageView card = (ImageView)LayoutInflater.from(WelcomeScreen.this).inflate(R.layout.card_view, null); 
      card.setImageResource(cards.get(i)); 
      card.setId(cards.get(i)); 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)(cardWidth*dp), LayoutParams.WRAP_CONTENT); 
      if (linearID != R.id.game_layout) params.addRule(RelativeLayout.ALIGN_PARENT_TOP, -1); 
      else params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1); 
      int leftMargin = (linearID == R.id.game_layout)? (int)((i*cardMarginVertical)*dp) : 0; 
      int topMargin = (linearID == R.id.game_layout)? 0 : (int)(((i % (10))*cardMarginVertical + 35)*dp); 
      int rightMargin = 0; 
      int bottomMargin = 0; 
      params.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 
      card.setLayoutParams(params); 
      if (linearID == R.id.game_layout) card.setOnClickListener(OnCardClick(card)); 
      myCardsLayout.addView(card); 
     } 
} 

這些卡片是粘在在屏幕的底部用params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1) 代碼行。

當用戶按下一個certian卡,所述的onClick函數被調用:

private View.OnClickListener OnCardClick(final ImageView card) { 
     return new View.OnClickListener() { 
      public void onClick(View v) { 
       RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)v.getLayoutParams(); 
       params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, (int)(10*dp)); 
      } 
     }; 
    } 

的問題是,這不會影響被按壓的卡的位置;他只是留在他的位置。 我猜ALIGN_PARENT_BOTTOM規則太「強」了,這就是爲什麼onClick函數不會影響卡。 任何人都可以想辦法解決這個問題嗎?

回答

2

我想通了。 對於那些誰在將來爲此努力奮鬥:

我已經包裹在一個RelativeLayout的卡的ImageView的,並把它放在一個單獨的佈局文件名my_card_view.xml下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"> 
    <ImageView android:layout_height="wrap_content" 
     android:layout_width="50dp" 
     android:scaleType="fitXY" 
     android:adjustViewBounds="true"> 
    </ImageView> 
</RelativeLayout> 

這被顯示在屏幕的底部的卡,是在運行時添加的,的onCreate期間:

fillMyCards(parseAndFillArray(myCardsArray)); 

fillMyCards的實現下面給出

private void fillMyCards(LinkedList<Integer> cards){ 
    int size = cards.size(); 
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.game_layout); 
    for (int i = 0 ; i < size ; i++){ 
     RelativeLayout card = (RelativeLayout)LayoutInflater.from(WelcomeScreen.this).inflate(R.layout.my_card_view, null); 
     card.setGravity(Gravity.CENTER); 
     ImageView cardImage = (ImageView)card.getChildAt(0); 
     cardImage.setImageResource(cards.get(i)); 
     card.setId(cards.get(i)); 
     RelativeLayout.LayoutParams cardParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     cardParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1); 
     int leftPadding =(int)((i*cardMarginHorizontal)*dp); 
     int topPadding = 0; 
     int rightPadding = 0; 
     int bottomPadding = 0; 
     card.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); 
     card.setLayoutParams(cardParams); 
     cardImage.setOnTouchListener(this); 
     layout.addView(card); 
    } 
} 

現在我們可以通過將下面的代碼的onClick改變紙牌的位置:

RelativeLayout parent = (RelativeLayout) view.getParent(); 
parent.setPadding(0, 0, 0, (int)(10*dp));