JLK*_*JLK 6 layout android overlap relativelayout android-framelayout
我试图让我的游戏中的扑克牌重叠,这样只能看到一张牌的前半部分而另一张牌被下一张扑克牌所覆盖.应该完全可见的唯一卡将是最后/最右边的卡.
我在framelayout和relativelayout中使用了以下代码无济于事.谁能提供一些建议?
public int shouldShow(int numberOfCards, int card, int id)
{
if(card == -1)
hide(id);
else
{
findViewById(id).setBackgroundDrawable(deckimages[card]);
//findViewById(id).offsetLeftAndRight(findViewById(id).getWidth()* numberOfCards / 2);
show(id);
//findViewById(id).setPadding(findViewById(id).getWidth()* numberOfCards / 2, 0,0,0);
return numberOfCards+1;
}
return numberOfCards;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用填充和偏移方法,这两种方法都不适合我.但我也注意到getwidth()和getmeasuredwidth()方法返回0.
我应该使用哪种布局以及为什么getwidth功能不起作用的任何建议?
xml代码在下面...会有比这更多的图像,但这是我正在测试的
<RelativeLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/RelativeLayout1">
<ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
多年来我一直坚持这种情况,经过4个小时搞乱代码和谷歌搜索.我终于找到了解决方案,而且非常简单.
您所要做的就是将下一张卡对齐上一张卡的顶部和左侧.这样就可以将第二张卡放在卡的顶部.然后设置a leftMargin将卡"推"到右上方,创建部分重叠效果.
这是我的代码(以编程方式完成):
for(int i=0; i<hand.size();i++){
int c = hand.get(i).getCardResource();
ib = new ImageButton(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_TOP, i-1);
params.addRule(RelativeLayout.ALIGN_LEFT,i-1);
params.leftMargin= 40;
ib.setImageResource(c);
ib.setClickable(true);
ib.setPadding( 3, 3, 3, 3);
ib.setId(i);
ib.setLayoutParams(params);
ll.addView(ib);
}
Run Code Online (Sandbox Code Playgroud)