问题描述
我正在向现有片段动态添加新片段,并且正在进行一些计算以推断新片段的大小。 获得新大小后,我更改了新片段的布局参数以匹配计算出的大小,这就是所有子视图消失的地方 - 如果我不更改大小,视图将完美呈现。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/playerCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#339CEF"
android:clickable="true"
android:focusable="true"
tools:context=".playerCard">
<ImageView
android:id="@+id/playerImage"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#DA4444"
android:clickable="false"
android:contentDescription="Todo"
android:focusable="true"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@drawable/ic_menu_camera"
tools:srcCompat="@drawable/ic_menu_camera" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="@color/teal_200"
android:clickable="false"
android:focusable="true"
android:text="TextView"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/playerImage" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#99A9D8"
android:clickable="false"
android:contentDescription="Todo"
android:focusable="true"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
这就是我在 onCreateView 上更改卡片大小的方法(检查了大小并且没问题 - 不是零):
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater,container,savedInstanceState)
var view = inflater.inflate(R.layout.fragment_player_card,false)
view.x = this.centerPoint?.x?.toFloat() ?: 0.0f
view.y = this.centerPoint?.y?.toFloat() ?: 0.0f
view.layoutParams.width = this.cardSize?.width ?: 0
view.layoutParams.height = this.cardSize?.height ?: 0
view.requestLayout()
view.setonTouchListener { v,event ->
onCardTouch(event)
}
return view
}
还尝试在 post
语句中更改大小,但得到了相同的结果。
任何帮助将不胜感激。
解决方法
请在此处使用此解决方案:
@Override
public void onResume() {
super.onResume();
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) getLayoutParameters());
}
private final ViewGroup.LayoutParams getLayoutParameters(){
ViewGroup.LayoutParams layoutParameters = getDialog().getWindow().getAttributes();
layoutParameters.width = MYCALCULATEDSIZE.Width;
layoutParameters.height = MYCALCULATEDSIZE.Height;
return layoutParameters;
}
确保在 Fragment 恢复时更新布局参数很重要,否则您可能会面临异常的风险,即视图尚未膨胀。 写在这里,确保片段及其子视图已正确膨胀。