Android - 在运行时更改ImageView位置

时间:2012-04-06 10:01:02

标签: android android-layout relativelayout

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

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功能不会影响卡。 谁能想到解决这个问题的方法呢?

1 个答案:

答案 0 :(得分: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));
相关问题