约束布局中的java嵌套
我是安卓应用开发新手,正在尝试构建一个简单的员工管理应用。目的是尽可能多地重用活动。现在,我基本上有四个活动——一个注册表,一个所有员工的列表(基本上使用单个员工记录布局的多个实例),一个更新记录,一个删除记录。我计划使用单个活动进行更新(将使用注册表的布局)和删除(将使用单个记录布局)操作。 为了实现这一点,我想启用注册表布局以进行更新,并禁用它,并使用附加的“删除”按钮启用单记录布局以删除记录,反之亦然。因为我使用的是约束布局,所以我希望包含一个容器,其中包含必要的布局
我尝试使用约束布局作为约束布局内部的容器,但当我尝试使用外部布局中的对象约束内部布局时,会产生问题。我试图用xml以及鼠标点击和拖动来编写约束。基本上,我无法用外部约束布局中的对象约束内部约束布局
我打算重用的更新/删除活动的Xml:
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:id="@+id/textView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginLeft="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginRight="8dp"
安卓:text="Enter ID"
安卓:textColor="#000"
app:layout_constraintBaseline_toBaselineOf="@+id/editText"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintStart_toStartOf="parent" />
<EditText
安卓:id="@+id/editText"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:layout_marginEnd="44dp"
安卓:layout_marginRight="44dp"
安卓:ems="10"
安卓:inputType="number"
安卓:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
安卓:background="@drawable/form_drawable"
安卓:paddingEnd="8dp"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingRight="8dp"
安卓:textColor="#FFF"/>
</安卓.support.constraint.ConstraintLayout>
用于单个员工记录的Xml:
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<安卓.support.v7.widget.CardView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<安卓.support.constraint.ConstraintLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:id="@+id/textView2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginLeft="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginRight="8dp"
安卓:text="Name"
安卓:textColor="#000"
app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
app:layout_constraintEnd_toStartOf="@+id/editText2"
app:layout_constraintStart_toEndOf="@+id/textView6" />
<EditText
安卓:id="@+id/editText2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:layout_marginEnd="64dp"
安卓:layout_marginRight="64dp"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:paddingRight="8dp"
安卓:textColor="#FFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
安卓:id="@+id/textView3"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginLeft="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginRight="8dp"
安卓:text="Designation"
安卓:textColor="#000"
app:layout_constraintBaseline_toBaselineOf="@+id/editText3"
app:layout_constraintEnd_toStartOf="@+id/editText3"
app:layout_constraintStart_toEndOf="@+id/textView6" />
<EditText
安卓:id="@+id/editText3"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="20dp"
安卓:layout_marginEnd="64dp"
安卓:layout_marginRight="64dp"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:paddingRight="8dp"
安卓:textColor="#FFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
<TextView
安卓:id="@+id/textView4"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginLeft="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginRight="8dp"
安卓:text="Address"
安卓:textColor="#000"
app:layout_constraintBaseline_toBaselineOf="@+id/editText4"
app:layout_constraintEnd_toStartOf="@+id/editText4"
app:layout_constraintStart_toEndOf="@+id/textView6" />
<EditText
安卓:id="@+id/editText4"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="20dp"
安卓:layout_marginEnd="64dp"
安卓:layout_marginRight="64dp"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:paddingRight="8dp"
安卓:textColor="#FFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText3" />
<TextView
安卓:id="@+id/textView5"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginLeft="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginRight="8dp"
安卓:text="Phone
"
安卓:textColor="#000"
app:layout_constraintBaseline_toBaselineOf="@+id/editText5"
app:layout_constraintEnd_toStartOf="@+id/editText5"
app:layout_constraintStart_toEndOf="@+id/textView6" />
<EditText
安卓:id="@+id/editText5"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="20dp"
安卓:layout_marginEnd="64dp"
安卓:layout_marginRight="64dp"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:paddingRight="8dp"
安卓:textColor="#FFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText4" />
<TextView
安卓:id="@+id/textView6"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginLeft="8dp"
安卓:layout_marginTop="20dp"
安卓:text="ID"
安卓:textColor="#000"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</安卓.support.constraint.ConstraintLayout>
</安卓.support.v7.widget.CardView>
</安卓.support.constraint.ConstraintLayout>
注册表格的Xml格式:
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<ScrollView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_margin="16dp"
安卓:orientation="vertical">
<TextView
安卓:id="@+id/label"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Fields marked with * are compulsory." />
<TextView
安卓:id="@+id/tvFirstName"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="First Name *"
安卓:textColor="@安卓:color/secondary_text_light"
安卓:textSize="18sp" />
<EditText
安卓:id="@+id/etFirstName"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:text="MAnish"
安卓:textColor="#FFF"
安卓:textSize="24sp" />
<TextView
安卓:id="@+id/tvMiddleName"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="Middle Name"
安卓:textColor="@安卓:color/secondary_text_light"
安卓:textSize="18sp" />
<EditText
安卓:id="@+id/etMiddleName"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:text="JOshi"
安卓:textColor="#FFF"
安卓:textSize="24sp" />
<TextView
安卓:id="@+id/tvLastName"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="Last Name *"
安卓:textColor="@安卓:color/secondary_text_light"
安卓:textSize="18sp" />
<EditText
安卓:id="@+id/etLastName"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:textColor="#FFF"
安卓:textSize="24sp" />
<TextView
安卓:id="@+id/tvAddress"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="Address *"
安卓:textColor="@安卓:color/secondary_text_light"
安卓:textSize="18sp" />
<EditText
安卓:id="@+id/etAddress"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPostalAddress"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:textColor="#FFF"
安卓:textSize="24sp" />
<TextView
安卓:id="@+id/tvPhone"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="Phone *"
安卓:textColor="@安卓:color/secondary_text_light"
安卓:textSize="18sp" />
<EditText
安卓:id="@+id/etPhone"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="phone"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:textColor="#FFF"
安卓:textSize="24sp" />
<TextView
安卓:id="@+id/tvDesignation"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:text="Designation *"
安卓:textColor="@安卓:color/secondary_text_light"
安卓:textSize="18sp" />
<EditText
安卓:id="@+id/etDesignation"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/form_drawable"
安卓:ems="10"
安卓:inputType="textPersonName"
安卓:paddingStart="8dp"
安卓:paddingLeft="8dp"
安卓:paddingEnd="8dp"
安卓:textColor="#FFF"
安卓:textSize="24sp" />
<Button
安卓:id="@+id/button5"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:text="SAVE"
安卓:layout_marginTop="32dp"
安卓:background="@drawable/crud_btns"
安卓:textColor="#FFF"
安卓:textSize="30sp"/>
</LinearLayout>
</ScrollView>
</安卓.support.constraint.ConstraintLayout>
在更新/删除布局中,我想永久输入一个id。 然后,根据所需的操作,我想显示注册表,其中所有字段都填写了以前的数据,以便对其进行更新。如果操作是删除,那么我只想显示具有给定ID的员工的详细信息(这样用户就不会删除错误的记录)以及删除按钮。 如有任何意见,将不胜感激
# 1 楼答案
这里没有使用嵌套的
constraint layout
。您使用的是三种不同的约束布局。你必须创建一个main约束布局,在该主布局中,你必须设计三个约束布局因此,基本上将所有三个
constraint layouts
都包含在一个主约束布局中