我想改变视图的高度和宽度,(可能是按钮视图、图像视图、线性布局、框架布局或任何类型的视图)。我使用方法 traverseRootViewAndSetLayoutParam(View, double , double) 来更改此操作。如果我评论了这些行,它就会起作用。
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