Android - 在运行时更改 ImageView 位置

标签 android android-layout android-relativelayout

我和我的同事正在尝试开发一款 Android 纸牌游戏。 某位玩家的牌显示在屏幕下方,对手的牌显示在屏幕的右上角和左上边缘:

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

我们非常希望添加以下功能: 当用户单击他的一张卡 (OnClickListener) 时,他选择的卡会比玩家的其他卡高出几个像素。 如果用户选择另一张牌,前一张牌将返回到其初始位置(屏幕底部),并且新牌会升起。

整个屏幕以相对布局表示:

<?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 函数不会影响卡片的原因。 谁能想出一个办法来解决这个问题吗?

最佳答案

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

我将卡片的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));

关于Android - 在运行时更改 ImageView 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041962/

相关文章:

java - 在 Android 中解析复杂的 WCF 响应

c# - Unity c#,截屏并保存为jpg文件

android - 入门所需的最小包,Android SDK

android - 带有图像和文本以及可检查属性的 ListView

android - 相对布局运行时的位置 View

java - Thread.currentThread().interrupt() 在 Android 中不工作

android - TextView 换行问题

android - PocketCasts 如何实现这种转换/效果?

android - ImageView 不拉伸(stretch)(Android)?

android - 如何使用 RelativeLayout 根的合并标签?