首页 > 解决方案 > 在 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

我尝试了不同的组合,但为什么没有显示徽标图像?

屏幕截图如下所示:

在此处输入图像描述

标签: android

解决方案


替换这些行:

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);

推荐阅读