在无父线性LinearLayout上设置LayoutParams

时间:2012-03-18 11:55:22

标签: android android-layout layoutparams

我以编程方式创建LinearLayout然后我想通过自定义编写的适配器将它附加到GridView。我想为我新创建的LinearLayout设置宽度和高度。当我尝试这样做时,我得到以下异常:

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)

这是我的代码:

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
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

使用GridView.LayoutParams试用。 LayoutParams必须是父级的参数。

public View getView(int position, View convertView, ViewGroup parent){
  LinearLayout layout = new LinearLayout(activity);
  layout.setLayoutParams(new GridView.LayoutParams(100, 100));
  return layout
}

顺便说一句convertView如果是!= null则应该使用。那更快。

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;
}

答案 1 :(得分:1)

LayoutParams是相对于父级的。

AbsListView.LayoutParams params = new AbsListView.LayoutParams(100,100);
layout.setLayoutParams(params);