android - 在 Android 中如何使用 Java 添加内部图像?
问题描述
我在“src\main\res\drawable\gate_logo.png”中有一个图像,如下所示:
我的 Java 代码如下所示:
LinearLayout Demo_Layout = new LinearLayout(context);
Demo_Layout.setId(View.generateViewId());
// Demo_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
Demo_Layout.setBackgroundColor(Color.rgb(98, 198, 238));
Demo_Layout.setHorizontalGravity(Gravity.CENTER);
Demo_Layout.setVerticalGravity(Gravity.CENTER);
addView(Demo_Layout, LinearLayout.LayoutParams.MATCH_PARENT,328);
TextView textView = new TextView(context);
textView.setId(View.generateViewId());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
textView.setText(Html.fromHtml("<P><Br><big>GATE Demo</big><P>[ Graphic Access Tabular Entry ]<Br><small>An Interception-resistant Authentication System</small>"));
// Demo_Layout.addView(textView, LinearLayout.LayoutParams.MATCH_PARENT, 328);
// Demo_Layout.addView(textView, 600, 328);
// int resID = getResources().getIdentifier("gate_logo.png", "drawable", "package.name");
int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);
// gateLogoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// gateLogoView.setScaleType(ImageView.ScaleType.FIT_XY);
gateLogoView.setLayoutParams( new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
gateLogoView.setX(1);
gateLogoView.setY(1);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
gateLogoView.setLayoutParams(vp);
gateLogoView.setImageResource(resID);
// Demo_Layout.addView(gateLogoView, 600, 328);
Demo_Layout.addView(gateLogoView);
LinearLayout Button_Layout = new LinearLayout(context);
Button_Layout.setId(View.generateViewId());
Button_Layout.setBackgroundColor(Color.rgb(168, 98, 188));
Button_Layout.setHorizontalGravity(Gravity.CENTER);
Button_Layout.setVerticalGravity(Gravity.CENTER);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 138);
lp1.addRule(RelativeLayout.BELOW, Demo_Layout.getId());
addView(Button_Layout, lp1);
Button Registration_Button=new Button(context);
Registration_Button.setText("Registration");
Registration_Button.setAllCaps(false);
Registration_Button.setOnClickListener(Registration_Demo_Listener);
Button_Layout.addView(Registration_Button);
Button Login_Button=new Button(context);
Login_Button.setText("Login");
Login_Button.setAllCaps(false);
Login_Button.setOnClickListener(Login_Demo_Listener);
Button_Layout.addView(Login_Button);
Button Auto_Demo_Button=new Button(context);
Auto_Demo_Button.setText("Auto Demo");
Auto_Demo_Button.setAllCaps(false);
Auto_Demo_Button.setOnClickListener(Auto_Demo_Listener);
Button_Layout.addView(Auto_Demo_Button);
我在同一目录中也有一半大小的图像小版本:gate_logo_s.png
我尝试了不同的组合,但为什么没有显示徽标图像?
屏幕截图如下所示:
解决方案
替换这些行:
int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);
用这些:
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(R.drawable.gate_logo);
推荐阅读
- node.js - 使用 puppeteer 使用元素的 onclick 跳转到下一页
- typescript - 使用 Font Awesome 和 Vue 的 Typescript 构建错误
- spring-boot - 如何覆盖测试类中的spring应用程序属性?使用 Kotlin 内部类时 Spring 的 @ContextConfiguration 中断
- python - 递归函数 - 计算元组中偶数的个数
- ruby - 如何检查Ruby中单词的第一个和最后一个字符是否相同?
- javascript - !function 在 webpack 缩小的 javascript 中做了什么
- mysql - 对于没有与另一个表连接记录的记录,如何获得连接结果?
- android - 尝试删除每个 Play 控制台的 Android SMS 和 CALL_LOG 权限,但找不到它们
- c++ - 在 cpp 应用程序中保存密码
- postgresql - Postgres 字符串函数(包括 regexp_*)是否将制表符视为空格?