我如何在Java中创建code的ImageView的,现有的布局中?布局、如何在、code、Java

2023-09-06 00:13:11 作者:綽辰

我在寻找一种简单的方法让用户看到他们已经有多少饮料有一个BAC计算器。

I'm looking for an easy way for the user to see how many drinks they've had for a BAC calculator.

照片应用程序的:

在按钮preSS,我想图像添加到屏幕上,直属微调,并与左对齐。当我再次preSS的按钮时,我想另一图像被添加到屏幕上。

On button press, I would like an image to be added to the screen, directly under the spinner and with left alignment. When I press the button again, I want another image to be added to the screen.

所以,如果我pressed的添加按钮,啤酒,啤酒的绘制将出现微调下方。如果我$ P $再次pssed附加啤酒按钮,我希望有两个可绘制啤酒的微调,$ P $下pferably与他们的权利被添加。

So if I pressed the add beer button, a drawable of a beer would appear below the spinner. If I pressed the add beer button again, I want there to be TWO drawables of beers under the spinner, preferably with them being added from the right.

(还有,让他们达到他们的宽度极限,缠绕,并在左边重新开始,但低于全行,将是真棒)

(Also, having them reach their width limit, wrapping around, and starting again on the left, but below a full line, would be AWESOME)

我无法弄清楚如何做到这一点。我认为增加的code进行的ImageView到相对布局(因为它需要被放置在右侧)将是最好的路线,但如果可能的话在XML中,我会更乐意使用它。任何帮助?

I can't figure out how to do this. I assume adding a ImageView in code to a relative layout (because it needs to be positioned to the right) would be the best route, but if it's possible in xml I'd be more than happy to use that. Any help?

推荐答案

在按钮的点击回调,创建一个ImageView的对象,置瓶中的图像,并将其定位。例如(我假设你的元素的名称):

In the button's click callback, create an ImageView object, set the bottle image, and position it. For example (I'm assuming the names of your elements):

ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.beerbottle);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.ButtonRecalculate);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(iv, lp);

我没有测试过这一点,你要知道,但它应该给你一个良好的开端。你可能需要在其他参数添加到ImageView的,可能到的LayoutParams,使它看起来不错,再加上跟踪有多少瓶都显示出来,等等。

I haven't tested this, mind you, but it should give you a good start. You'll probably need to add other parameters to the ImageView and possibly to the LayoutParams to make it look good, plus tracking how many bottles are displayed, etc.

 
精彩推荐
图片推荐