Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.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 在主活动中显示图像的问题_Android_Imageview - Fatal编程技术网

Android 在主活动中显示图像的问题

Android 在主活动中显示图像的问题,android,imageview,Android,Imageview,我试图以编程方式在主活动上显示图像。我以前在ContentFragment上成功地实现了这一点,但由于某些原因,在成功构建之后,图像不在那里 我的示例代码: LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View myView = inflater.inflate(R.layout.activity_main, null); View

我试图以编程方式在主活动上显示图像。我以前在ContentFragment上成功地实现了这一点,但由于某些原因,在成功构建之后,图像不在那里

我的示例代码:

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View myView = inflater.inflate(R.layout.activity_main, null);
    ViewGroup mainLayout = (RelativeLayout) myView;

    ImageView wView = new ImageView(this);
    Drawable W = ContextCompat.getDrawable(this, R.drawable.w);

    RelativeLayout.LayoutParams imageParam =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

    wView.setImageDrawable(W);                        
    imageParam.setMargins(100, 100, 0, 0);
    mainLayout.addView(wView);
    wView.setLayoutParams(imageParam);   

在Activity中,我们通过调用:
setContentView(R.layout.Activity\u main)在Activity的onCreate方法中膨胀视图

如果要以编程方式将图像添加到此视图,请尝试首先获取活动的根视图:

ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
然后将图像添加到此视图:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);

        ImageView wView = new ImageView(this);
        Drawable W = ContextCompat.getDrawable(this, R.drawable.w);

        RelativeLayout.LayoutParams imageParam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageParam.setMargins(100, 100, 0, 0);
        wView.setLayoutParams(imageParam);
        wView.setImageDrawable(W);
        viewGroup.addView(wView);
    }
}

您需要设置其高度和宽度:

ImageView wView = new ImageView(this);
((ViewGroup.LayoutParams)(wView.getLayoutParams())).width = 200;
((ViewGroup.LayoutParams)(wView.getLayoutParams())).height = 200;

非常感谢,这正是我想要的。此外,我还喜欢您如何堆叠RelativeLayout初始化。那看起来整洁多了。谢谢你回答我的问题,但我担心这没有让图像显示出来。CookieMonster的实现工作,但我很高兴你分享了你的代码,因为我以后需要调整高度和宽度参数。