问题
I creating LinearLayout programatically then I want to attach it to GridView via custom written adapter. I want to set width and height to my newly created LinearLayout. When I try to do so I get following exception:
03-18 15:50:54.648: E/AndroidRuntime(2708): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.GridView.onMeasure(GridView.java:937)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.View.measure(View.java:8313)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.os.Looper.loop(Looper.java:130)
03-18 15:50:54.648: E/AndroidRuntime(2708): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 15:50:54.648: E/AndroidRuntime(2708): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:50:54.648: E/AndroidRuntime(2708): at java.lang.reflect.Method.invoke(Method.java:507)
03-18 15:50:54.648: E/AndroidRuntime(2708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 15:50:54.648: E/AndroidRuntime(2708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 15:50:54.648: E/AndroidRuntime(2708): at dalvik.system.NativeStart.main(Native Method)
Here is my code:
public View getView(int position, View convertView, ViewGroup parent){
LinearLayout layout = new LinearLayout(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
layout.setLayoutParams(params);
return layout
}
Any help will be appreciated.
回答1:
Try it with GridView.LayoutParams
. The LayoutParams have to be the params of the parent.
public View getView(int position, View convertView, ViewGroup parent){
LinearLayout layout = new LinearLayout(activity);
layout.setLayoutParams(new GridView.LayoutParams(100, 100));
return layout
}
btw the convertView
should be used if it is != null
. That's faster.
public View getView(int position, View convertView, ViewGroup parent){
View result = convertView;
if (result == null) {
result = new LinearLayout(activity);
result.setLayoutParams(new GridView.LayoutParams(100, 100));
}
// set attributes based on position here
return result;
}
Edit:
// using ViewGroup
public View getView(int position, View convertView, ViewGroup parent){
ViewGroup result = null;
if (convertView instanceof ViewGroup) {
result = (ViewGroup) convertView;
} else {
// that will never happen if you never return anything but ViewGroups
}
if (result == null) {
// since LinearLayout is a (extends) ViewGroup
result = new LinearLayout(activity);
result.setLayoutParams(new GridView.LayoutParams(100, 100));
}
// set attributes based on position here
return result;
}
回答2:
LayoutParams is relative to the parent.
AbsListView.LayoutParams params = new AbsListView.LayoutParams(100,100);
layout.setLayoutParams(params);
来源:https://stackoverflow.com/questions/9758078/set-layoutparams-on-parentless-linearlayout