RelativeLayout
을 사용하고 ALIGN_PARENT_BOTTOM
규칙과 bottomMargin
을 설정하여 카드 스택을 동적으로 만들려고합니다. 그러나 카드는 여백을 무시하고 계속해서 그려지고 있습니다. 하지만 ALIGN_PARENT_TOP
과 topMargin
을 사용할 때 제대로 그려집니다.Java 코드를 통해 RelativeLayout.LayoutParams의 아래쪽 여백 설정이 작동하지 않습니다.
activity_single_player_game.xml (코드 카드 묶음을 만드는)
<!-- 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)); } } } }
일부 캡쳐 화면 있습니다 (코드 RelativeLayout
을 만드는)
ALIGN_PARENT_TOP
과 topMargin
을 사용할 때의 결과입니다. 나는 동그라미가있는 카드가 텍스트와 함께 있어야한다는 것을 제외하고는 정확히 이것을 원한다.
이 내가 ALIGN_PARENT_BOTTOM
및 bottomMargin
를 사용할 때 내가 무엇을 얻을 수 있습니다. 여기에 카드는 그룹과 같이 올바른 위치에 있지만 모두 서로 완전히 그려져 있기 때문에 하나만 볼 수 있습니다.
'playerView'는 카드를 포함하는'RelativeLayout'입니다. 나는 이것을 보여주기 위해 코드를 업데이트했다. 'android : layout_marginBottom'이 작동하지 않는 이유는'android : layout_marginTop'가 작동하는 이유는 무엇입니까? 제안한 방법으로 새 카드를 추가 할 때마다 기존 스택의 각 카드를 업데이트해야합니다. 더 좋은 방법이 없을까요? –
[여기] (http://stackoverflow.com/questions/8642071/why-marginbottom-not-working). 더 나은 해결책은 아래쪽 패딩이있는'ViewGroup' 1st 안에'imageView'를 래핑 한 다음 ** ViewView에 ** ViewView **를 추가하는 것입니다. ** –
감사합니다. 'wrap_content' 높이가 필요하지 않으므로'match_parent'로 변경했습니다. 이제 잘 작동합니다. :) –