Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中,如何使用Java添加内部图像?_Android - Fatal编程技术网

在Android中,如何使用Java添加内部图像?

在Android中,如何使用Java添加内部图像?,android,Android,我在“src\main\res\drawable\gate\u logo.png”中有一个图像,如下所示: LinearLayout Demo_Layout = new LinearLayout(context); Demo_Layout.setId(View.generateViewId()); // Demo_Layout.setBackgroundColor(Color.rgb(88, 188, 218)); Demo_Layout.setBackground

我在“src\main\res\drawable\gate\u logo.png”中有一个图像,如下所示:

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

我的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);
LinearLayout Demo_Layout=新的LinearLayout(上下文);
Demo_Layout.setId(View.generateViewId());
//Demo_布局.setBackgroundColor(Color.rgb(88188218));
Demo_布局.setBackgroundColor(Color.rgb(98198238));
演示布局。设置水平重力(重心);
演示布局。设置垂直重力(重心);
addView(演示布局,LinearLayout.LayoutParams.MATCH\u父级,328);
TextView TextView=新的TextView(上下文);
setId(View.generateViewId());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
textView.setText(Html.fromHtml(“


GATE Demo

[Graphic Access Tabular Entry]
一种防拦截的身份验证系统”); //Demo_Layout.addView(textView,LinearLayout.LayoutParams.MATCH_父项,328); //Demo_Layout.addView(textView,600328); //int resID=getResources().getIdentifier(“gate_logo.png”、“drawable”、“package.name”); int resID=getResources().getIdentifier(“gate_logo_.png”,“drawable”,context.getPackageName()); ImageView gateLogoView=新的ImageView(上下文); gateLogoView.setImageResource(resID); //gateLogoView.setScaleType(ImageView.ScaleType.CENTER\U裁剪); //gateLogoView.setScaleType(ImageView.ScaleType.FIT_XY); gateLogoView.setLayoutParams(新视图组.LayoutParams(LayoutParams.MATCH_父级,LayoutParams.MATCH_父级)); gateLogoView.setX(1); gateLogoView.setY(1); LinearLayout.LayoutParams vp=新的LinearLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容); gateLogoView.setLayoutParams(vp); gateLogoView.setImageResource(resID); //Demo_Layout.addView(gateLogoView,600328); Demo_Layout.addView(gateLogoView); LinearLayout按钮\布局=新的LinearLayout(上下文); 按钮_Layout.setId(View.generateViewId()); 按钮布局。设置背景颜色(颜色。rgb(168,98,188)); 按钮布局。设置水平重力(重心); 按钮布局。设置垂直重力(重心); RelativeLayout.LayoutParams lp1=新的RelativeLayout.LayoutParams(LayoutParams.MATCH_父项,138); lp1.addRule(RelativeLayout.down,Demo_Layout.getId()); addView(按钮布局,lp1); 按钮注册\按钮=新按钮(上下文); 注册按钮设置文本(“注册”); 注册按钮。设置所有上限(错误); 注册按钮。setOnClickListener(注册演示监听器); 按钮\布局。添加视图(注册\按钮); 按钮登录\按钮=新按钮(上下文); Login_Button.setText(“登录”); 登录按钮。setAllCaps(false); 登录按钮。setOnClickListener(登录演示监听器); 按钮\布局。添加视图(登录\按钮); 按钮自动演示按钮=新按钮(上下文); 自动演示按钮.setText(“自动演示”); 自动演示按钮。设置所有上限(错误); 自动演示按钮。setOnClickListener(自动演示监听器); 按钮布局。添加视图(自动演示按钮);

在同一个目录中,我还有一个小一半的图像版本:gate_logo_.png

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

屏幕截图如下所示:

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

更换这些线路:

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

src/main/res中的图像通常位于assets文件夹中。看看如何加载它们。显示如何访问您的可绘图资源。@Robert
src/main/res/assets
下的资源与
src/main/res/drawable
下的资源不同。