How to set layout_width of an image pragmatically in android
I am trying to set layout_width to FILL_PARENT to an image view. I am using following code::
ImageView image = new ImageView(mContext);
image.setImageResource(R.drawable.my_ac_line);
LayoutParams lp = new Lay开发者_开发百科outParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
image.setLayoutParams(lp);
layout.addView(image);
But its not working.
Can any please suggest me the answer for it.
Thanks in advance.
You might be importing LayoutParams
import android.view.ViewGroup.LayoutParams;
instead it try
import android.widget.TableRow.LayoutParams;
Hope it will help you.
Actually, it won't help to set plain LayoutParams, you need to create/set specialized LayoutParams (RelativeLayout.LayoutParams / LinearLayout.LayoutParams / etc) depending on what kind of LayoutParams is used by layout where your view is added.
Each Layout class has own specialized LayoutParams class and work only with this. So the answer depends on class of your "layout" object. Fix by above advice.
精彩评论