嗨,我有一个我想多次使用的布局
但是当我使用include时它只出现一次,我在网上看了但无法得到答案
你能建议我在这里做错了什么吗
提前致谢 R
我的常用布局
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="match_parent">
<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_constraintEnd_toStartOf="@+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_constraintEnd_toStartOf="@+id/tv_denomination"
app:layout_constraintStart_toEndOf="@+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_constraintStart_toEndOf="@+id/et_denomination_count"
app:layout_constraintTop_toTopOf="@+id/et_denomination_count" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
片段.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/denominations_layout"
app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till" />
<include
android:id="@+id/denomination_twenty"
layout="@layout/denominations_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>
您可以尝试将每个放在里面并在 FrameLayout 上设置所需的尺寸
<FrameLayout
android:id="@+id/denomination_fifty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till">
<include
layout="@layout/denominations_layout" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/denomination_fifty">
<include
android:id="@+id/denomination_twenty"
layout="@layout/denominations_layout" />
</FrameLayout>
要使约束起作用,您必须 add
layout_width
和 layout_height
到 <include
标签。
<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:layout_marginVertical="18dp"
android:text="Cash in till"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/denomination_fifty"
layout="@layout/denominations_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till" />
<include
android:id="@+id/denomination_twenty"
layout="@layout/denominations_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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>