我有一个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)
我错过了什么?