我在 fragment Activity 中设置 ImageView 的高度和宽度时遇到问题。我正在使用布局参数。我的代码是这样的
View rootView = inflater.inflate(R.layout.book_details_layout, container, false);
pdf_image = ((ImageView) rootView.findViewById(R.id.pdf_img));
pdf_image.setLayoutParams(new LayoutParams(80, 80));
我在这段代码上崩溃了。 logcat 就像
E/AndroidRuntime(7517): java.lang.ClassCastException:android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
E/AndroidRuntime(7517): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1722)
E/AndroidRuntime(7517): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1667)
E/AndroidRuntime(7517): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:373)
E/AndroidRuntime(7517): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
如何解决这个问题?
最佳答案
显然,您的父 View 是一个 RelativeLayout
,您尝试根据它在您的 ImageView
上使用 LayoutParams
。这可能会导致 ClassCastException
。试试这个:
pdf_image = ((ImageView) rootView.findViewById(R.id.pdf_img));
RelativeLayout.LayoutParams paramImage = new RelativeLayout.LayoutParams(80,80);
pdf_image.setLayoutParams(paramImage);
如果这有帮助,请告诉我。
关于java - 使用 LayoutParam 在 android Fragment 中动态设置 ImageView 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23004951/