gpt4 book ai didi

android - 以编程方式显示 ImageView

转载 作者:IT老高 更新时间:2023-10-28 13:23:11 26 4
gpt4 key购买 nike

当用户单击按钮时,如何使 ImageView 出现在屏幕中间?我已将图像放在 res/drawable-folder 中。

我一直在尝试使用下面的代码,但我不知道如何使 ImageView 出现:

 View v = new ImageView(getBaseContext());
ImageView image;
image = new ImageView(v.getContext());
image.setImageDrawable(v.getResources().getDrawable(R.drawable.gameover));

谢谢!

最佳答案

//LinearLayOut Setup
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

//ImageView Setup
ImageView imageView = new ImageView(this);

//setting image resource
imageView.setImageResource(R.drawable.play);

//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));

//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);

关于android - 以编程方式显示 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com