如何动态地将 ImageView 添加到 View

发布于 2024-12-03 12:14:20 字数 323 浏览 1 评论 0原文

我想制作一副纸牌。我有一个名为 Deck (Deck 类扩展 View)的视图,我已将其设置为 ContentView,并且我想动态地将卡片作为 ImageView 添加到 Deck 上。我搜索了很多(谷歌,论坛),但找不到一种可以理解它是如何工作的方法,所以我想我会问这个菜鸟问题..

如果我有 ImageView 卡,我该如何添加它到视图平台以及如何指定它的确切放置位置?我不想创建 XML 布局,因为那样我就必须在 xml 上手动添加 52 张卡片作为 ImageView。我想动态地执行此操作,但布局处理让我感到困惑。感谢帮助或指点,谢谢。另外,我不想使用画布,因为我无法确定哪个卡位图位于另一个卡位图之上。

I want to create a deck of cards. I have a view called Deck (class Deck extends View) which I have set as the ContentView and I want to add the cards as ImageViews on the Deck, dynamically. I have searched a lot (google,forums) and I can't find a way that I can understand how it works, so I thought I'd ask this noob question..

If I have an ImageView card, how do I add it to the View deck and how do I specify where exactly it should be placed? I don't want to create an XML layout, because then I will have to add 52 cards by hand on the xml as ImageViews. I want to do this dynamically, and the layout handling has me confused. Help or pointers appreciated, thanks. Also, I dont want to use a canvas because I have no way of determining which card bitmap is on top of another.

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-12-10 12:14:20

我将这样做:

Deck deck = new Deck(this); // Create initial deck view
int iterations = 10; // Defined card count

for (int i = 0; i < iterations; i++) {
    Card card = new Card(this); // Create a new card (extends ImageView)
    card.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // Set some layout parameters
    card.setImageResource(R.drawable.card_blank); // Set the cards image
    deck.addView(card); // Add it to the deck
}

Here is how I would do it:

Deck deck = new Deck(this); // Create initial deck view
int iterations = 10; // Defined card count

for (int i = 0; i < iterations; i++) {
    Card card = new Card(this); // Create a new card (extends ImageView)
    card.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // Set some layout parameters
    card.setImageResource(R.drawable.card_blank); // Set the cards image
    deck.addView(card); // Add it to the deck
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文