本文介绍了在父母的一套的LinearLayout的LayoutParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建的LinearLayout编程然后我想通过定制编写的适配器将其附加到GridView控件。我想的宽度和高度设置为我的新创建的LinearLayout。当我尝试这样做,我得到以下异常:
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)
下面是我的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
}
任何帮助将AP preciated。
Any help will be appreciated.
推荐答案
与 GridView.LayoutParams
试试吧。该的LayoutParams必须是父母的PARAMS。
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的 convertView
应,如果它是被用来!= NULL
。这是更快的。
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;
}
编辑:
// 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;
}
这篇关于在父母的一套的LinearLayout的LayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!