使用与线性布局结合并设置图像,但透明视图的ontop(图像)

发布于 2025-01-24 03:40:57 字数 1502 浏览 0 评论 0原文

我已经有两天的时间遇到​​问题了...在单击按钮时,我有一个绑定和图像可以弹出。 我希望每次按下按钮时,另一个图像都会在Pervious旁边弹出,或者,如果线性布局中没有空间,则它将弹出前一个图像。 这个想法是要从卡甲板上获取卡片,并将其放在玩家的手中。

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPlayTableBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        var image = findViewById<ImageView>(R.id.fight_deck)
        var i: Int = 1
        val deck = Deck()
        var deckListOfCards: MutableList<Card> = deck.shuffledDeckOfCard()
        val deck_clickable_top = findViewById<ImageButton>(R.id.deck_button)
        val imageView = ImageView(this)
        val Width = convertDpToPixel(135f, this)
        val Height = convertDpToPixel(190f, this)
        imageView.layoutParams = LinearLayout.LayoutParams(Width.toInt(), Height.toInt())
        var imgResId: Int = 0
        deck_clickable_top.setOnClickListener {
            if (deckListOfCards.size != 0) {
                imgResId = deckListOfCards.get(0).getCardFace()
                i += 1
                deck.removeCardFromDeck(deckListOfCards.get(0))
            }
            if (deckListOfCards.size == 0) {
                deck_clickable_top.visibility = View.INVISIBLE
                deck_clickable_top.layoutParams.height = 0
            }
            imageView.setImageResource(imgResId)
        }
        binding.bottomLinearLayout.addView(imageView)
    }

我不知道该怎么做...

I'm having an issue with this for two days now... I have a binding and an image to pop when clicking a button.
I want that every time the button is pressed another image will pop either next to the pervious one, or, if there's no space in the linear layout, that it will pop ontop of the previous one.
The idea is to get a card from a card deck and put it ot the hand of the player.

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPlayTableBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        var image = findViewById<ImageView>(R.id.fight_deck)
        var i: Int = 1
        val deck = Deck()
        var deckListOfCards: MutableList<Card> = deck.shuffledDeckOfCard()
        val deck_clickable_top = findViewById<ImageButton>(R.id.deck_button)
        val imageView = ImageView(this)
        val Width = convertDpToPixel(135f, this)
        val Height = convertDpToPixel(190f, this)
        imageView.layoutParams = LinearLayout.LayoutParams(Width.toInt(), Height.toInt())
        var imgResId: Int = 0
        deck_clickable_top.setOnClickListener {
            if (deckListOfCards.size != 0) {
                imgResId = deckListOfCards.get(0).getCardFace()
                i += 1
                deck.removeCardFromDeck(deckListOfCards.get(0))
            }
            if (deckListOfCards.size == 0) {
                deck_clickable_top.visibility = View.INVISIBLE
                deck_clickable_top.layoutParams.height = 0
            }
            imageView.setImageResource(imgResId)
        }
        binding.bottomLinearLayout.addView(imageView)
    }

I can't figure out how to do it...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-31 03:40:57

好吧,Linearlayout似乎并不是一个好选择,但是您可以尝试使用这些台词:

// step 1 - this is the linear layout you'll use as the container
val containerBinding = CustomLinearLayoutContainerBinding.inflate(inflater, container, false)

// step 2 - on some click event you can create a new binding, this should contain you image
val componentBinding = CustomComponentLayoutBinding.inflate(inflater, container, false).apply {
     // set the image drawable here, text, any other data in here
}

// step 3 - the root layout of that binding is the linear layout
containerBinding.root.addView(componentBinding.root)

// the following 2 methods of LinearLayout can be very handy
removeAllViews() // to remove all previously added children
invalidate() // to re-render the linear layout

但是请注意,在添加它们后,您想使用这些视图的所有内容都需要手动更改,而且这不是感觉你应该去做。但是,我发现此链接,查看此实现,感觉这更适合您的要求。

Well, LinearLayout doesn't seem like a good choice for this, but you can try something along these lines:

// step 1 - this is the linear layout you'll use as the container
val containerBinding = CustomLinearLayoutContainerBinding.inflate(inflater, container, false)

// step 2 - on some click event you can create a new binding, this should contain you image
val componentBinding = CustomComponentLayoutBinding.inflate(inflater, container, false).apply {
     // set the image drawable here, text, any other data in here
}

// step 3 - the root layout of that binding is the linear layout
containerBinding.root.addView(componentBinding.root)

// the following 2 methods of LinearLayout can be very handy
removeAllViews() // to remove all previously added children
invalidate() // to re-render the linear layout

But please note that everything you want to do with those views after you added them will need manual changes and again, this doesn't feel like you should go for it. However, I found this link, check out this implementation, feels like this suits your requirements better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文