【发布时间】:2021-11-21 06:20:02
【问题描述】:
如何克隆布局中特定视图的属性?
我有这个观点
<com.google.android.material.textview.MaterialTextView
android:id="@+id/fragment_login_main_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:fontFamily="@font/font_en_medium"
android:gravity="center"
android:text="@string/fragment_login_main_text"
android:textColor="@color/fragment_login_text1_main_text"
android:textSize="22.5sp" />
我想创建这个视图的克隆,我就是这样做的
MaterialTextView materialTextView = fragmentLoginBinding.fragmentLoginMainText;
materialTextView.setId(View.NO_ID);
fragmentLoginBinding.getRoot().addView(materialTextView);
但我得到了这个错误
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
所以我想知道是否有任何方法可以从该视图创建克隆,而无需像这样手动进行
MaterialTextView materialTextView = new MaterialTextView(requireActivity());
materialTextView.setText(fragmentLoginBinding.fragmentLoginMainText.getText());
materialTextView.setTypeface(fragmentLoginBinding.fragmentLoginMainText.getTypeface());
materialTextView.setTextSize(fragmentLoginBinding.fragmentLoginMainText.getTextSize());
etc...
fragmentLoginBinding.getRoot().addView(materialTextView);
【问题讨论】:
标签: android view android-view