1、動(dòng)態(tài)設(shè)置LayoutParams時(shí)候
動(dòng)態(tài)設(shè)置LayoutParams時(shí)候,子View設(shè)置的ViewGroup的LayoutParams 錯(cuò)誤
2、自定義View中的id與xml 中的id沖突
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to androidx.fragment.app.FragmentContainerView
案例:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/com_c14_alpha_100">
<com.xqhy.common.base.view.title.TitleBar
android:id="@+id/title"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:enable_text_color="@color/com_c1_alpha_100"
app:title_bg="@color/com_c14_alpha_100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/my_fans" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container"
android:layout_width="@dimen/dp_0"
android:layout_height="@dimen/dp_0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>
自定義View的TitleBar,其中使用了ViewBinding加載布局
public class TitleBar extends FrameLayout {
private TitleBarBinding mBinding;
//省略代碼...
public TitleBar(@NonNull Context context) {
this(context, null);
}
public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
isHideTitle = typedArray.getBoolean(R.styleable.TitleBar_hide_title, false);
mTitleStr = typedArray.getString(R.styleable.TitleBar_title);
mBackIcon = typedArray.getResourceId(R.styleable.TitleBar_back_icon, 0);
mBackgroundColor = typedArray.getColor(R.styleable.TitleBar_title_bg, ContextCompat.getColor(context, R.color.com_page_background));
isShowMore = typedArray.getBoolean(R.styleable.TitleBar_show_more, false);
isHideLine = typedArray.getBoolean(R.styleable.TitleBar_hide_line, false);
typedArray.recycle();
}
initView();
clickListener();
}
private void initView() {
mBinding = TitleBarBinding.inflate(LayoutInflater.from(getContext()), this, true);
mContainer = mBinding.container;
//省略代碼...
}
TitleBarBinding對(duì)應(yīng)的title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/com_c14_alpha_100"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_44">
<ImageView
android:id="@+id/back"
android:layout_width="@dimen/dp_44"
android:layout_height="@dimen/dp_44"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dp_2"
android:padding="@dimen/dp_10"
android:src="@drawable/ic_direction_left" />
</RelativeLayout>
<View
android:id="@+id/view_line"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:background="@color/com_c1_alpha_6" />
</LinearLayout>
這里可以看出自定義布局中的xml也包含了container的id,與頁(yè)面布局中的FragmentContainerView的id沖突,這個(gè)問(wèn)題主要是在遍歷的時(shí)候由于id相同,導(dǎo)致創(chuàng)建的View錯(cuò)誤,從而造成ClassCastException