java - Android 用不同的视图替换视图但保留原始约束
问题描述
所以我有一个视图,我想用另一个视图替换它。到目前为止,我已经做到了。
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 中被夸大但是代码从不运行 MyCustomView.Java 只运行 XML。
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)
推荐阅读
- bnf - EBNF 到 BNF 转换为表达式的 <<'while'>> ::= while <'Z'> do begin <'A'> { <'A'> } end
- angular - 如何使用带有参数的流来构造 Angular Service 方法以选择一个实体?
- python - Python:如何读取每行都是字符串的 CSV 文件?
- java - 使用 Spring + Thymeleaf 从表中的选定复选框中获取 Id
- vb6 - 获得编译错误:VB6 中未定义子或函数错误
- logging - 如何在 ubuntu 18.04 中管理 /var/log/journal/ 中的文件?
- php - 如何在 WordPress 中将复杂的 JSON 格式转换为可读格式?
- reactjs - 在单击按钮时反应延迟加载动态导入加载组件
- c# - C# WCF gzip 压缩 IIS 无法使 svc/gzip 工作(使用 CloudFlare)
- python - 为什么在使用scrapy的时候会出现这个参数错误?