简体   繁体   English

如何在Android上显示RelativeLayout和ImageView?

[英]How to display RelativeLayout and ImageView on Android?

I have added a RelativeLayout and ImageView dynamically, but it's not displaying in the emulator when I run the application. 我已经动态添加了RelativeLayout和ImageView,但是当我运行该应用程序时,它并没有显示在模拟器中。 Why? 为什么?

My code: 我的代码:

RelativeLayout relativeLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(     
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    ImageView iv = new ImageView(this);         
    iv.setImageResource(R.drawable.freshface_mmm);
    relativeLayout.addView(iv,lp);

您需要使用以下方法将相对布局添加到活动中:

    setContentView(view, params);

如果要添加视图,则应使用此方法:

ViewGroup relativeLayout = new RelativeLayout(getActivity());

将ImageView的位置设置为其父对象的中心,左侧,底部等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM