我想要更改视图的高度和宽度,(可能是按钮视图,图像视图,linearlayout,framelayout或任何类型的视图)。我使用方法traverseRootViewAndSetLayoutParam(View, 双,双),改变这样做。如果我对这些行进行评论,它的工作原理。
if (view.getClass().equals(LinearLayout.class)) {
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);
} else if (view.getClass().equals(FrameLayout.class)) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);
}
,但如果我运行的所有代码都比我在onCreate()方法中获得ClassCastException,
我调试代码,方法traverseRootViewAndSetLayoutParam(View,double,double)没有给出异常,但是在控制传递了onCreate()之后,它给出了ClassCastException。我检查控件是否在线,System.outprintln(>> oncreate end>>)。此时我没有收到任何错误,但是会发生穿越oncreate方法异常。
我在onCreate方法中调用的代码行,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
...
...
try {
traverseRootViewAndSetLayoutParam(rootLinearLayout, widthRatio,
heightRatio);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(">>> oncreate end >>> ");
}
traverseRootViewAndSetLayoutParam()方法,
public void traverseRootViewAndSetLayoutParam(View view,
double heightRatio, double widthRatio) {
System.out.println(">>> iCount >>> " + iCount++);
if (view.getClass().equals(LinearLayout.class)) {
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);
} else if (view.getClass().equals(FrameLayout.class)) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);
}
// else {
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
// (int) (((double) widthRatio) * (view.getWidth())),
// (int) (((double) heightRatio) * (view.getHeight())));
// view.setLayoutParams(layoutParams);
// }
if (view instanceof LinearLayout || view instanceof FrameLayout) {
if (view instanceof LinearLayout) {
System.out.println(">>> LinearLayout Class >>> "
+ view.getClass());
int childcount = ((LinearLayout) view).getChildCount();
for (int i = 0; i < childcount; i++) {
View childView = ((LinearLayout) view).getChildAt(i);
traverseRootViewAndSetLayoutParam(childView, heightRatio,
widthRatio);
}
} else if (view instanceof FrameLayout) {
System.out.println(">>> FrameLayout Class >>> "
+ view.getClass());
int childcount = ((FrameLayout) view).getChildCount();
for (int i = 0; i < childcount; i++) {
View childView = ((FrameLayout) view).getChildAt(i);
traverseRootViewAndSetLayoutParam(childView, heightRatio,
widthRatio);
}
}
}
}
错误日志,
12-16 00:30:12.791: W/dalvikvm(277): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-16 00:30:12.952: E/AndroidRuntime(277): FATAL EXCEPTION: main
12-16 00:30:12.952: E/AndroidRuntime(277): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureVertical(LinearLayout.java:355)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.View.measure(View.java:8171)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.View.measure(View.java:8171)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.View.measure(View.java:8171)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.View.measure(View.java:8171)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.View.measure(View.java:8171)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.View.measure(View.java:8171)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
12-16 00:30:12.952: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 00:30:12.952: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 00:30:12.952: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
12-16 00:30:12.952: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 00:30:12.952: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 00:30:12.952: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
12-16 00:30:42.751: I/Process(277): Sending signal. PID: 277 SIG: 9
答案 0 :(得分:3)
您将收到此异常,因为LayoutParams始终相对于父视图组。
e.g。你有一个RelativeLayout,在这个RelativeLayout里面你有一个LinearLayout,这个LinearLayout的LayoutParams将是一个RelativeLayout.LayoutParams。
那是因为这些参数与父在其区域内布局视图的方式有关。
答案 1 :(得分:1)
if (view.getClass().equals(LinearLayout.class)) {
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);
} else if (view.getClass().equals(FrameLayout.class)) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);
}
在这里,您正在检查视图的类型,而不是视图的父级。如果视图的父级是LinearLayout
且视图本身是FrameLayout
,那么您将给它FrameLayout.LayoutParams
,这是不正确的。
由于您没有使用权重或任何特定于布局类型的内容,我建议您使用ViewGroup.LayoutParams
:
ViewGroup.LayoutParams layoutParams = new
ViewGroup.LayoutParams(
(int) (((double) widthRatio) * (view.getWidth())),
(int) (((double) heightRatio) * (view.getHeight())));
view.setLayoutParams(layoutParams);