Android中<include>复用布局仅显示一次的问题排查请求
问题分析与解决
问题现象
复用布局denomination_layout.xml通过<include>标签在fragment中引用两次,最终仅显示一次。
核心问题与修复步骤
1. 根布局高度导致覆盖
denomination_layout.xml的根ConstraintLayout高度设为match_parent,第一个include会占满整个屏幕,第二个include被推到屏幕之外,自然无法显示。
修复:
将根布局的android:layout_height改为wrap_content:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 修改此处 -->
2. 布局文件引用拼写错误
你在fragment的include标签中写的是@layout/denominations_layout(多了一个s),但实际布局文件名是denomination_layout.xml,这会导致系统找不到正确的布局文件。
修复:
修正include中的布局引用:
<include android:id="@+id/denomination_fifty" layout="@layout/denomination_layout" <!-- 去掉多余的s --> app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till" /> <include android:id="@+id/denomination_twenty" layout="@layout/denomination_layout" <!-- 同样修正 --> app:layout_constraintTop_toBottomOf="@+id/denomination_fifty" />
3. 内部控件约束冲突
denomination_layout.xml中的EditText存在约束逻辑错误:
app:layout_constraintEnd_toStartOf="@+id/tv_denomination" app:layout_constraintStart_toEndOf="@+id/tv_denomination_total"
这会让EditText同时试图靠向左侧TextView的左边和右侧TextView的右边,导致控件位置异常甚至不显示。
修复:
调整EditText的约束,让三个控件横向排列:
<EditText android:id="@+id/et_denomination_count" android:layout_width="100dp" android:layout_height="wrap_content" android:hint="0" android:inputType="number" app:layout_constraintStart_toEndOf="@+id/tv_denomination" <!-- 改为靠左侧TextView右侧 --> app:layout_constraintEnd_toStartOf="@+id/tv_denomination_total" <!-- 改为靠右侧TextView左侧 --> app:layout_constraintTop_toTopOf="parent" />
修复后的完整代码
denomination_layout.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" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/tv_denomination" android:layout_width="wrap_content" android:layout_height="match_parent" android:hint="£0.00" app:layout_constraintBottom_toBottomOf="@+id/et_denomination_count" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/et_denomination_count" /> <EditText android:id="@+id/et_denomination_count" android:layout_width="100dp" android:layout_height="wrap_content" android:hint="0" android:inputType="number" app:layout_constraintStart_toEndOf="@+id/tv_denomination" app:layout_constraintEnd_toStartOf="@+id/tv_denomination_total" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tv_denomination_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="£0.00" app:layout_constraintBottom_toBottomOf="@+id/et_denomination_count" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@+id/et_denomination_count" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
fragment.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" tools:context=".SecondFragment"> <TextView android:id="@+id/tv_Cash_in_till" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cash in till" android:layout_marginVertical="18dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/denomination_fifty" layout="@layout/denomination_layout" app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till" /> <include android:id="@+id/denomination_twenty" layout="@layout/denomination_layout" app:layout_constraintTop_toBottomOf="@+id/denomination_fifty" /> <Button android:id="@+id/button_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/previous" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者BRDroid
相关产品推荐
相关产品推荐

