【问题标题】:How to overlap imageviews in a layout如何在布局中重叠图像视图
【发布时间】:2017-04-05 21:32:17
【问题描述】:

我遇到了视图之间的空间问题。使用 LinerLayout 并将视图(ImageViews)动态添加到线性布局。

我想调整 imageview 之间的空间。我希望它们有重叠 - 当添加新视图时。

Current view of the app

目前应用蓝色背景来突出显示两个线性布局。

将图像视图添加到现有布局的代码

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);

}

点击“Hit”按钮是在调用 handleHit() 时。并且添加的新图像使从 2nd 到当前视图的所有图像都不可见。我只能看到第一个和最后一个(最新添加的)。

【问题讨论】:

    标签: android android-layout android-view


    【解决方案1】:

    LinearLayout 一个接一个地以线性方式堆叠视图。您永远不能仅使用LinearLayout 创建重叠布局,它将始终根据定义的方向将其堆叠在当前视图旁边。

    您正在寻找的是相对布局中的RelativeLayout,视图相互堆叠,并且由于它也是ViewGroup 类,因此相同的代码也适用于它。

    查看相关布局文档here 和他们比较的简短帖子here

    此外,当您在相对布局中创建子级时,您可以将参数添加到它所在的位置,或者简单地添加一个为原始布局大小一半的边距以从中心堆叠它们!

    您不必调用更新参数,而是将参数添加到图像视图。

      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()中的代码部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多