通过Java代码设置RelativeLayout.LayoutParams中的底部边距不起作用

时间:2014-07-01 06:17:41

标签: android android-layout layoutparams android-relativelayout

我正在尝试使用RelativeLayout并设置ALIGN_PARENT_BOTTOM规则和bottomMargin来动态创建一叠卡片。但是不管利润率如何,这些卡片都会被吸引过来。但是当我使用ALIGN_PARENT_TOPtopMargin时,它们会正确绘制。

activity_single_player_game.xml (代码创建RelativeLayout

<!-- other code -->

    <RelativeLayout
        android:id="@+id/frame_card_stack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom" >

        <ImageView
            android:id="@+id/image_hand_value"
            android:layout_width="40dip"
            android:layout_height="20dip"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dip" />

        <TextView
            android:id="@+id/text_hand_value"
            android:layout_width="40dip"
            android:layout_height="20dip"
            android:gravity="center"
            android:layout_alignParentBottom="true"
            android:background="#88000000" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/frame_card_stack_split"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:gravity="bottom" >

        <ImageView
            android:id="@+id/image_hand_value_split"
            android:layout_width="40dip"
            android:layout_height="20dip"
            android:layout_alignParentBottom="true" />

        <TextView
            android:id="@+id/text_hand_value_split"
            android:layout_width="40dip"
            android:layout_height="20dip"
            android:gravity="center"
            android:layout_alignParentBottom="true"
            android:background="#88000000" />

    </RelativeLayout>

<!-- other code -->

SinglePlayerGameActivity.java (创建卡片堆的代码)

RelativeLayout playerView;
RelativeLayout playerViewSplit;

@Override
protected void OnCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_player_game);

    playerView = (RelativeLayout) findViewById(R.id.frame_card_stack);
    playerViewSplit = (RelativeLayout) findViewById(R.id.frame_card_stack_split);
}

public ImageView getCardImage(BJCard card, int offset, boolean playerCard) {
    ImageView cardImage = new ImageView(this);
    cardImage.setImageResource(BJUtility.drawableIdForCard(card));
    cardImage.setBackgroundColor(0x40000000);
    if (playerCard) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.bottomMargin = dip(20 * offset);
        cardImage.setLayoutParams(params);
    } else {
        LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        cardImage.setLayoutParams(params);
    }
    return cardImage;
}

public void populatePlayer() {
    BJPlayerBox box = MenuActivity.bjmc.playerBoxes.get(MenuActivity.bjmc.activePlayerBox);
    BJCardDeck deck = box.cardDecks.get(box.activeDeckIndex);
    for (int i = 0; i < deck.cards.size(); i++) {
        BJCard card = deck.cards.get(i);
        playerView.addView(getCardImage(card, i, true), playerView.getChildCount() - 2);
    }
    updatePlayerSumView(deck, BEGIN, box.activeDeckIndex);
}

public void updatePlayer(int flag) {
    BJPlayerBox box = MenuActivity.bjmc.playerBoxes.get(MenuActivity.bjmc.activePlayerBox);
    switch (flag) {
    case HIT:
        BJCardDeck deck = box.cardDecks.get(box.activeDeckIndex);
        for (int i = playerView.getChildCount() - 2; i < deck.cards.size(); i++) {
            BJCard card = deck.cards.get(i);
            playerView.addView(getCardImage(card, i, true), playerView.getChildCount() - 2);
        }
        updatePlayerSumView(deck, flag, box.activeDeckIndex);
        break;
    case SPLIT:
        playerViewSplit.setVisibility(android.view.View.VISIBLE);
        ImageView splitCardView = (ImageView) playerView.getChildAt(playerView.getChildCount() - 2);
        playerView.removeView(splitCardView);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.bottomMargin = dip(0);
        playerViewSplit.addView(splitCardView, playerViewSplit.getChildCount() - 2);
        splitCardView.setLayoutParams(params);
        // update first deck
        BJCardDeck splitDeck = box.cardDecks.get(0);
        BJCard card = splitDeck.cards.get(1);
        playerView.addView(getCardImage(card, 1, true), playerView.getChildCount() - 2);
        updatePlayerSumView(splitDeck, SPLIT, 0);
        // update split deck
        splitDeck = box.cardDecks.get(1);
        card = splitDeck.cards.get(1);
        playerViewSplit.addView(getCardImage(card, 1, true), playerViewSplit.getChildCount() - 2);
        updatePlayerSumView(splitDeck, SPLIT, 1);
        break;
    case SURRENDER:
        updatePlayerSumView(box.activeCardDeck(), flag, box.activeDeckIndex);
    }
}

public void updatePlayerSumView(BJCardDeck deck, int flag, int splitIndex) {
    ImageView iv;
    TextView tv;
    if (splitIndex == 0) {
        iv = (ImageView) findViewById(R.id.image_hand_value);
        tv = (TextView) findViewById(R.id.text_hand_value); 
    } else {
        iv = (ImageView) findViewById(R.id.image_hand_value_split);
        tv = (TextView) findViewById(R.id.text_hand_value_split); 
    }
    if (flag == SURRENDER) {
        tv.setText(null);
        iv.setImageResource(R.drawable.flag);
        return;
    }
    if (flag != SPLIT && BJUtility.isBlackjack(deck)) {
        iv.setImageResource(R.drawable.crown);
    } else {
        int[] playerSum = BJUtility.currentCardHandTotal(deck.cards);
        if (playerSum[0] > 21) {
            tv.setText(null);
            iv.setImageResource(R.drawable.bust);
        } else {
            if (playerSum.length == 1) {
                tv.setText(Integer.toString(playerSum[0]));
                tv.setTextColor(getResources().getColor(R.color.orangish_yellow));
            } else {
                // TODO: if deckState = active, show both, else, show one
                tv.setText(playerSum[1] + "/" + playerSum[0]);
                tv.setTextColor(getResources().getColor(R.color.orangish_yellow));
            }
        }
    }
}

以下是一些截图 -

What I want

这是我使用ALIGN_PARENT_TOPtopMargin时获得的结果。我想要的确是这样,除了带圆圈的卡片应该用文字记下来。

What I get

这是我使用ALIGN_PARENT_BOTTOMbottomMargin时获得的结果。卡片在这里作为一个组处于正确的位置,但它们都完全相互绘制,所以我只能看到一个。

1 个答案:

答案 0 :(得分:0)

你的 playerView ??

是什么?

如果是 RelativeLayout android:layout_height="wrap_content"android:layout_marginBottom无效。

由于您无法将android:paddingBottom=""设置为 playerView ,因此您可以尝试为之前的子视图设置android:layout_marginTop

类似的事情,在添加第二张卡的同时将android:layout_marginTop添加到第一张卡片。