简体   繁体   English

如何为ImageView设置LayoutParams

[英]How to set LayoutParams for ImageView

I'm new to Android. 我是Android的新手。 In my app I have created an ImageView programmatically in my FrameLayoutProgramatically class but when I use setLayoutParams of this Imageview there it's showing errors. 在我的应用程序中,我已经在我的FrameLayoutProgramatically类中以编程方式创建了一个ImageView,但是当我使用此Imageview的setLayoutParams ,它显示错误。 Why am I getting these errors? 为什么我会收到这些错误?

My FrameLayoutProgramatically class: 我的FrameLayoutProgramatically类:

public class FrameLayoutProgramatically extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Initializing imageView
        ImageView imageview = new ImageView(this);
        imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageview.setImageResource(R.drawable.nature);
        imageview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}

You need to specify which LayoutParams you need to use, it must be based on parent layout which can be anything LinearLayout.LayoutParams or RelativeLayout.LayoutParams or FrameLayout.LayoutParams . 您需要指定需要使用的LayoutParams ,它必须基于父布局,可以是LinearLayout.LayoutParamsRelativeLayout.LayoutParamsFrameLayout.LayoutParams

RelativeLayout imageLayout = new RelativeLayout(this);

ImageView imageview = new ImageView(this);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setImageResource(R.drawable.nature);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

imageLayout.addView(iv, lp);

or 要么

imageview.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
imageLayout.addView(iv);

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

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