1

我想以Imageview编程方式创建一个而不使用XML. 有没有办法使用高度、宽度和边距属性来创建它。

是否可以使用高度、宽度和边距属性将这样的多个组件添加到屏幕的不同位置?

4

4 回答 4

0
            LinearLayout linearLayout= new LinearLayout(this);
                    linearLayout.setOrientation(LinearLayout.VERTICAL);

                    linearLayout.setLayoutParams(new LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT));

                    //ImageView Setup
                    ImageView imageView = new ImageView(this);
                    //setting image resource
                    imageView.setImageResource(R.drawable.something);
                    //setting image position
//set the margin to the layoutparams
    LinearLayout.LayoutParams lp = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,      LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(left, top, right, bottom);
                    imageView.setLayoutParams(lp);

                    //add view to layout
                    linearLayout.addView(imageView);
                    //make visible to program
                    setContentView(linearLayout);
于 2013-10-16T09:36:25.247 回答
0

我写了一些你可以使用的代码。它依赖于您的容器是RelativeLayout这一事实:

ImageView myImageView = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.topMargin = myTopMargin;
params.leftMargin = myLeftMargin;

myImageView.setLayoutParams(params);
myImageView.setImageResource(R.id.my_image_resource);

myRelativeLayout.addview(myImageView);

希望这可以帮助 :)

于 2013-10-16T09:30:37.320 回答
0

尝试这个..

imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

 LinearLayout linearLayout= new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.VERTICAL);

            linearLayout.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));

            //ImageView Setup
            ImageView imageView = new ImageView(this);
            //setting image resource
            imageView.setImageResource(R.drawable.play);
            //setting image position
            imageView.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT));

//setting image margin
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
imageView.setLayoutParams(layoutParams);

            //adding view to layout
            linearLayout.addView(imageView);
            //make visible to program
            setContentView(linearLayout);

希望这会有所帮助..

于 2013-10-16T09:32:39.790 回答
0

用这个:

LinearLayout linearLayout= new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        linearLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));

        //ImageView Setup
        ImageView imageView = new ImageView(this);
        //setting image resource
        imageView.setImageResource(R.drawable.play);
        //setting image position
        imageView.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT));

        //adding view to layout
        linearLayout.addView(imageView);
        //make visible to program
        setContentView(linearLayout);
于 2013-10-16T09:32:47.793 回答