提问者:小点点

视图与include标记一起使用时不可见


我有一个布局activity_main.xml,它使用include标记添加了另一个布局(login_screen.xmlsignup_screen.xml)。 使用include标记添加的布局具有使用view声明的视图组件。 该视图在它们各自的布局(login_screen.xmlsignup_screen.xml)中是可见的,但是当添加到activity_main.xml中时,View将变得不可见。 我尝试复制整个代码并将其粘贴到activity_main.xml中,当时view可见。

实际问题是什么? 谁能帮帮我吗?

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/logo"/>

        <TextView
            android:id="@+id/app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"/>

        <include
            layout="@layout/login_screen"
            android:id="@+id/login_screen"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <include
            layout="@layout/signup_screen"
            android:id="@+id/signup_screen"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <com.google.android.material.button.MaterialButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/as_guest"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

login_screen.xml

<androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <View
          android:layout_width="2dp"
          android:layout_height="match_parent"/>

      <EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:hint="Username" />

      <EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:hintt="Password"/>
</androidx.constraintlayout.widget.ConstraintLayout>

signup_screen.xml

<androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <View
          android:layout_width="2dp"
          android:layout_height="match_parent"/>

      <EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:hint="Username" />

      <EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:hintt="Password"/>
</androidx.constraintlayout.widget.ConstraintLayout>

共1个答案

匿名用户

如果在标记上将android:layout_widts设置为match_parent,是否有效?

您现在实际上说的是“好的,让宽度匹配设置的任何约束”,但是没有任何约束要匹配。

注意:对于当前的XML,您可以使用LinearLayout,并将Android:orient设置为Vertical-您目前没有指定任何约束。