1

我有一个RelativeLayout与xml类似的东西:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/appHolder"
  android:layout_width="210dp"
  android:layout_height="100dp"
  android:background="@drawable/app_container_box"
  android:paddingBottom="10dp"
  android:paddingLeft="15dp"
  android:paddingRight="10dp"
  android:paddingTop="10dp" >
  ...
</RelativeLayout>

我想根据某些条件以编程方式修改此布局的宽度。

GetView()方法中,我有:

public View getView(int position, View recycledView, ViewGroup viewGroup) {
    View row = recycledView;
    Holder holder;
    row = inflater.inflate(R.layout.app_icon_tile, viewGroup, false);
    if (row == null) {
        /*
        * I added only the following `if` block
        * The rest is older code and was working fine
        */
        if (changeWidth) {
            row.setLayoutParams(new RelativeLayout.LayoutParams(260, 100));
        }
        holder = new Holder();
        holder.icon = (ImageView) row.findViewById(R.id.gridAppIcon);
        holder.appTitle = (TextView) row.findViewById(R.id.gridAppTitle);
        row.setTag(holder);
    } else {
        holder = (Holder) row.getTag();
    }
    return (row);
}

但是在运行此代码时,我的应用程序崩溃并显示以下堆栈跟踪:

FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
       at android.widget.GridView.onMeasure(GridView.java:937)
       at android.view.View.measure(View.java:8314)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
       at android.view.View.measure(View.java:8314)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
       at android.view.View.measure(View.java:8314)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       at android.view.View.measure(View.java:8314)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
       at android.view.View.measure(View.java:8314)
       at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
       at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:130)
       at android.app.ActivityThread.main(ActivityThread.java:3683)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:507)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
       at dalvik.system.NativeStart.main(Native Method)

我错过了什么?

4

1 回答 1

3

你的问题出在这里:

if (changeWidth) {
       row.setLayoutParams(new RelativeLayout.LayoutParams(260, 100));
}

因为当您设置布局参数时,它们应该是父级的类型,而不是您为其创建参数本身的视图。

因此,例如,如果您RelativeLayout在 a 中ListView,它应该得到ListView.LayoutParams.

于 2012-12-14T08:41:01.507 回答