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 に答える