Android 如何在布局中重叠图像视图
我面临着两种观点之间的空间问题。使用线性布局并将视图(图像视图)动态添加到线性布局 我想调整imageview之间的空间。我希望它们在添加新视图时具有重叠 目前,蓝色背景应用于突出显示两个线性布局 将图像视图添加到现有布局的代码Android 如何在布局中重叠图像视图,android,android-layout,android-view,Android,Android Layout,Android View,我面临着两种观点之间的空间问题。使用线性布局并将视图(图像视图)动态添加到线性布局 我想调整imageview之间的空间。我希望它们在添加新视图时具有重叠 目前,蓝色背景应用于突出显示两个线性布局 将图像视图添加到现有布局的代码 dealerImages= (LinearLayout) findViewById(R.id.dealerImages); dealerImages.setBackgroundColor(Color.BLUE); ImageView view = new I
dealerImages= (LinearLayout) findViewById(R.id.dealerImages);
dealerImages.setBackgroundColor(Color.BLUE);
ImageView view = new ImageView(BlackJack.this);
view.setImageResource(R.drawable.back);
dealerImages.addView(view);
每次添加新视图时,我都要指定与旧视图的相对位置。我希望新视图从布局中最后一个视图的中心开始
如果您需要任何其他详细信息,请告诉我。请建议,如果我需要使用任何其他布局,使事情更容易
编辑-在此发布代码
playerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
dealerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
private void dealTwoCardsEach(){
player.addCard(hit());
ImageView imageView = new ImageView(BlackJack.this);
imageView.setImageResource(getResourceId(player.getFirstCard()));
playerImages.addView(imageView, player.getCards().size()-1);
dealer.addCard(hit());
imageView = new ImageView(BlackJack.this);
imageView.setImageResource(getResourceId(dealer.getFirstCard()));
dealerImages.addView(imageView);
player.addCard(hit());
if(player.getCount() == 21)
player.setBlackJack(true);
imageView = new ImageView(BlackJack.this);
imageView.setImageResource(getResourceId(player.getSecondCard()));
updateMarginForPlayer(); // updating start margin
playerImages.addView(imageView, player.getCards().size()-1, playerLayoutParams);
dealer.addCard(hit());
}
private void updateMarginForPlayer(){
playerLayoutParams.setMarginStart(playerLayoutParams.getMarginStart()+100);
}
请不要,我没有为玩家的第一张牌设置任何边距。
我可以看到这两张牌,直到这里。第一幅图像的起始页边距为0,第二幅图像的起始页边距为100
private void handleHit(){
Card c1 = hit();
player.addCard(c1);
ImageView imageView = new ImageView(this);
imageView.setImageResource(getResourceId(c1));
updateMarginForPlayer();
playerImages.addView(imageView, playerLayoutParams);
}
在调用handleHit()时单击“Hit”按钮。添加的新图像使从第二张到当前视图的所有图像都不可见。我只能看到第一个和最后一个(最新添加的)。A
LinearLayout
以线性方式一个接一个地堆叠视图。您永远不能仅使用LinearLayout
创建重叠布局,它将始终根据定义的方向将其堆叠在当前视图旁边
您要查找的是相对布局中的RelativeLayout
,视图彼此独立堆叠,并且由于它也是一个ViewGroup
类,因此相同的代码也适用于它
查看相关布局文档,并简要介绍它们的比较
此外,当您在相对布局中创建一个子布局时,您可以将参数添加到它所在的位置,或者只需添加一个边距,该边距为原始布局的一半,即可将它们从中心堆叠起来
您不必调用更新参数,而是将参数添加到imageview
ImageView imageView = new ImageView(this);
imageView.setImageResource(getResourceId(c1));
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(100,0,0,0);
imageView.setLayoutParams(layoutParams);
playerImages.addView(imageView, playerLayoutParams);
非常感谢。这似乎奏效了。但是现在在指定的边距处添加了一个新视图后,我看不到它下面的视图。我只能看到第一个和最新的。在添加子视图之前,是否需要将属性设置为始终可见。它们不可见,因为它们可能位于上一个视图的下方,您能否发布代码,以便我可以为您编辑它!我用代码编辑了原始问题。请看一看。检查编辑。检查handleHit()中的代码部分