布局更新不会膨胀整个布局



我试图将这个约束布局扩展到另一个约束布局。

但是当我运行它时,我只得到了粉红色的卡片,四个ImageView没有出现在卡片内。

这里是主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_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/wall"
tools:context=".StepThreeActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="374dp"
android:layout_height="635dp"
android:background="@drawable/card"
android:backgroundTint="@color/white"
android:elevation="@dimen/cardview_default_elevation"
android:padding="13dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="11dp"
android:backgroundTint="@color/Blue"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/back" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="11dp"
android:backgroundTint="@color/Blue"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/forward" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这里是与前面主XML相关的java类

public class StepThreeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step_three);
initialization();
characterInitialization();

LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.card1,
constraintLayout, true);  
}

这里是card1.xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2c"
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_width="wrap_content"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="347dp"
android:layout_height="366dp"
android:layout_marginTop="16dp"
android:background="@drawable/card"
android:backgroundTint="#FBD5D8"
android:elevation="@dimen/cardview_default_elevation"
android:padding="13dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="111dp"
android:layout_height="149dp"
android:layout_marginStart="40dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@mipmap/a1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="106dp"
android:layout_height="150dp"
android:layout_marginStart="172dp"
android:layout_marginTop="168dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@mipmap/a2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="112dp"
android:layout_height="152dp"
android:layout_marginStart="40dp"
android:layout_marginTop="164dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@mipmap/a3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="115dp"
android:layout_height="153dp"
android:layout_marginStart="172dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@mipmap/a4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

4张卡片图片没有出现的问题,因为你实际上没有将它们添加到布局中。

from card1.xml:

tools:srcCompat="@mipmap/a1"
tools:srcCompat="@mipmap/a2"
tools:srcCompat="@mipmap/a3"
tools:srcCompat="@mipmap/a4"

当你使用tools命名空间时,你只会影响android studio布局验证窗格中的布局,这不会影响到你运行应用程序时的布局。

有关tools命名空间的更多信息,您可以查看文档。

要解决这个问题,您需要将tools替换为app名称空间。所以你的card1。xml布局将是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2c"
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_width="wrap_content"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="347dp"
android:layout_height="366dp"
android:layout_marginTop="16dp"
android:background="@drawable/card"
android:backgroundTint="#FBD5D8"
android:elevation="@dimen/cardview_default_elevation"
android:padding="13dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="111dp"
android:layout_height="149dp"
android:layout_marginStart="40dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/a1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="106dp"
android:layout_height="150dp"
android:layout_marginStart="172dp"
android:layout_marginTop="168dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/a2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="112dp"
android:layout_height="152dp"
android:layout_marginStart="40dp"
android:layout_marginTop="164dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/a3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="115dp"
android:layout_height="153dp"
android:layout_marginStart="172dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/a4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

你真的需要用这种方式填充布局吗?

你就不能包括这个布局吗

<include layout="@layout/card1 ... />

整个XML文件:

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="374dp"
android:layout_height="635dp"
android:background="@drawable/card"
android:backgroundTint="@color/white"
android:elevation="@dimen/cardview_default_elevation"
android:padding="13dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<include layout="@layout/card1 ... />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="11dp"
android:backgroundTint="@color/Blue"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/back" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="11dp"
android:backgroundTint="@color/Blue"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/forward" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>