Эй, ребята, я пытаюсь отобразить пользовательское сообщение, внутри моего приложения, но я получаю эту ошибку во время установки отступов в макете programaticaly:Android: Fatal Exception: java.lang.ClassCastException LayoutParams
Fatal Exception:java.lang.ClassCastExceptionandroid.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
Вот как я пытаюсь установить отступы:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if(alignTop){
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getContext().getResources().getDisplayMetrics());
}
params.setMargins(0, actionBarHeight, 0,0);
}else{
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
MessageView.this.setLayoutParams(params);
А вот мой XML-файл:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:roboto="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e1e1e"
android:id="@+id/messageViewBackground"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/messageViewSpinnerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<ImageView
android:id="@+id/messageViewSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_spinner_small"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_spinner_logo_small"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<ImageView
android:id="@+id/messageViewIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@android:drawable/stat_sys_warning"
android:visibility="gone"/>
<com.myapp.widgets.RobotoText
android:id="@+id/messageViewText"
roboto:font="Light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5B5B5B"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
Что я делаю неправильно?
Попробуйте установить FrameLayout.LayoutParams вместо этого, если RelativeLayout.LayoutParams. В этом аналогичном вопросе: http://stackoverflow.com/questions/11544964/framelayout-to-relativelayout-classcastexception-even-if-there-is-no-framelayout –
Если вы посмотрите на мой код, вы увидите, что Я делаю схожие вещи с тем, что вы предлагаете, и не работает. – user5035668
Нет, вы по-прежнему используете RelativeLayout.LayoutParms вместо FrameLayout.LayoutParams. Вы читали ответы? – Zarwan