我正在使用此代码。
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;
但我有app崩溃。我如何改变我的imageView布局的宽度和高度? THX。
答案 0 :(得分:0)
Resources r = getResources();
如果您使用主要活动,请使用此代码 使用上下文值只有像这样的上下文值
Context ct;//in class
Context ct=MainActivity.this;//in oncreate() menthod
Resource r =ct.getResource();//in your code
或者如果您将Context ct设为
static Context ct;//in class
ct=MainAcitivity.this;// in oncreate() function
调用您的代码
Resource r=MainAcitivity.ct.getResource();
但将上下文存储为stactic变量并不是一个好习惯,除非它最后一个选项只能像这样使用
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;