我正在创建一个自定义Gridview,每个单元格由一个包含
的布局表示我在适配器的getView()中对它们进行了膨胀。
它的工作正常,所述布局是Linear类型。但是如果我用Frame / relative布局替换它,它会在Logcat中崩溃。
04-20 18:19:16.921: E/AndroidRuntime(24111): FATAL EXCEPTION: main
04-20 18:19:16.921: E/AndroidRuntime(24111): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1303)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1250)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:281)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:303)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.View.measure(View.java:8313)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.GridView.onMeasure(GridView.java:950)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.View.measure(View.java:8313)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.View.measure(View.java:8313)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.View.measure(View.java:8313)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.os.Looper.loop(Looper.java:123)
04-20 18:19:16.921: E/AndroidRuntime(24111): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-20 18:19:16.921: E/AndroidRuntime(24111): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 18:19:16.921: E/AndroidRuntime(24111): at java.lang.reflect.Method.invoke(Method.java:507)
04-20 18:19:16.921: E/AndroidRuntime(24111): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-20 18:19:16.921: E/AndroidRuntime(24111): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-20 18:19:16.921: E/AndroidRuntime(24111): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
所有布局类(LinearLayout,RelativeLayout等)都扩展了ViewGroup。
ViewGroup类有两个静态内部类:LayoutParams和MarginLayoutParams。 ViewGroup.MarginLayoutParams实际上扩展了ViewGroup.LayoutParams。
有时布局类需要额外的布局信息才能与子视图相关联。为此,他们定义了内部静态LayoutParams类。例如,LinearLayout具有:
public class LinearLayout extends ViewGroup {
...
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
...
}
}
RelativeLayout也是如此:
public class RelativeLayout extends ViewGroup {
...
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
...
}
}
但是LinearLayout.LayoutParams和RelativeLayout.LayoutParams是完全不同的独立类。它们存储有关子视图的不同附加信息。
例如,LinearLayout.LayoutParams可以将权重值与每个视图相关联,而RelativeLayout.LayoutParams则不能。与RelativeLayout.LayoutParams相同:它可以将上面,下面的值,alightWithParent与每个视图相关联。并且LinearLayout.LayoutParams根本就没有这些功能。
因此,通常,您必须使用LayoutParams来封装布局,以使视图正确定位和渲染。但请注意,所有LayoutParams都具有相同的父类ViewGroup.LayoutParams。如果您只使用该类中定义的功能(例如WRAP_CONTENT和FILL_PARENT),您可以获得正常工作的代码,即使使用了错误的LayoutParams类来指定布局参数。