2

我和我的同事正在尝试为安卓开发一款纸牌游戏。某个玩家的牌显示在屏幕的底部,他的对手的牌显示在屏幕的右上角和左上角:

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) 卡片用代码行粘在屏幕底部。

当用户按下某个卡片时,会调用 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 函数不会影响卡片的原因。谁能想到解决这个问题的方法?

4

1 回答 1

2

我想到了。对于那些将来会为此苦苦挣扎的人:

我已将卡片的 ImageView 包装在 RelativeLayout 中,并将其放在名为 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));
于 2012-04-07T10:18:44.663 回答