我正在使用这个代码。
Resources r = MainActivity.ct.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics());
View inflatedLayout = getLayoutInflater().inflate(R.layout.list_row_layout, null, false);
inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().width *= px / 540;
inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().height *= px / 960;但我有应用程序崩溃。我如何改变宽度和高度我的imageView布局?谢谢。
发布于 2015-07-19 18:06:32
Resources r = getResources();如果u R调用MAINACTIVITY,则使用此代码,否则只使用上下文值,如下所示
Context ct;//in class
Context ct=MainActivity.this;//in oncreate() menthod
Resource r =ct.getResource();//in your code或者在您的情况下,如果您将上下文ct作为
static Context ct;//in class
ct=MainAcitivity.this;// in oncreate() function调用您的代码
Resource r=MainAcitivity.ct.getResource();但是,将上下文存储为静态变量并不是一个很好的做法,除非最后一种选择只能这样使用。
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics());
View inflatedLayout = getLayoutInflater().inflate(R.layout.list_row_layout, null, false);
inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().width *= px / 540;
inflatedLayout.findViewById(R.id.thumbImage).getLayoutParams().height *= px / 960;https://stackoverflow.com/questions/31503093
复制相似问题