所以我有一个要替换为其他视图的视图。到目前为止,我已经做到了。
View child = fragmentView.findViewById(R.id.view1);
ViewGroup parent = (ViewGroup) child.getParent();
int index = parent.indexOfChild(child);
parent.removeView(child);
child = new MyCustomView(this.getContext());
parent.addView(child, index);
虽然这可行。它还删除了约束,因此新视图最终出现在左上角。我似乎找不到一种方法来存储放置在原始视图上的约束并允许新视图使用相同的约束。
更新:
我尝试了以下内容
View child = fragmentView.findViewById(R.id.view1);
ViewGroup.LayoutParams childParams = child.getLayoutParams();
ViewGroup parent = (ViewGroup) child.getParent();
int index = parent.indexOfChild(child);
parent.removeView(child);
child = new MyCustomView(this.getContext());
parent.addView(child, index,childParams);
它似乎在正确的轨道上,但还不完全正确。
XML:
我通过以下方式将我的自定义视图包括在我的片段xml中:
<include
android:id="@+id/view1"
layout="@layout/my_custom_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
my_custom_view XML看起来像这样
<?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:layout_height="wrap_content" android:layout_width="match_parent">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="B1"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/timeLeftText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/peopleText" />
<TextView
android:id="@+id/peopleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/5"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/timeLeftText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="19:30"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:src="@mipmap/ic_launcher_round"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
应该在MyCustomView.java中夸大此视图,但是该代码永远不会只运行XML的MyCustomView.Java。
MyCustomView.java看起来像这样。
public class MyCustomView extends ConstraintLayout {
public MyCustomView(@NonNull Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_custom_view, this, true);
}
}
您可以在XML中设置两个视图,也可以在活动/片段/设置可见性的任何位置设置
view1.setVisibility(View.VISIBLE)
view2.setVisibility(View.GONE)