我是 Android 新手。在我的应用程序中,我在我的 FrameLayoutProgramatically 类中以编程方式创建了一个 ImageView,但是当我使用 setLayoutParams 时 这个 Imageview 那里显示错误。为什么我会收到这些错误?
我的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.
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);
或
imageview.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
imageLayout.addView(iv);
关于android - 如何设置ImageView的LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35954616/