【问题标题】:View is not visible while using it with include tag将视图与包含标签一起使用时不可见
【发布时间】:2020-11-21 10:35:50
【问题描述】:

我有一个布局 activity_main.xml,它使用 include 标签添加了另一个布局 (login_screen.xml signup_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>

编辑:我已经删除了这里的所有约束以使其更易于阅读,在我的代码中,我已经正确设置了约束并且它是完美的。

【问题讨论】:

  • 嗨,Kavin,我也遇到了类似的问题,能否请您更新一下您是如何解决的?

标签: android xml view include


【解决方案1】:

如果您在&lt;include /&gt; 标签上将android:layout_width 设置为match_parent 是否有效?

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

注意:对于您当前的 XML,您不妨使用 LinearLayout 并将 android:orientation 设置为 vertical - 您目前没有指定任何约束。

【讨论】:

  • 感谢您的即时回复。我尝试添加match_parent 仍然看起来一样。但是在编辑layout_width=""时,我一直这样android:layout_width="",此时View是可见的,但是include标签中包含的整个布局会跳到屏幕顶部。
【解决方案2】:

现在,我不确定是什么问题,但对于您的用例,您可以使用 ViewStub 而不是 include

【讨论】:

    【解决方案3】:

    &lt;include&gt; 标签的布局设置约束对我有用

      <include
        android:id="@+id/login_screen"
        layout="@layout/login_screen"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    
    <include
        android:id="@+id/signup_screen"
        layout="@layout/signup_screen"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    

    您是否在活动中的任何地方更改约束?

    【讨论】:

    • 我已经正确添加了约束,但为了简单起见,我没有在问题中提到。正如你提到的,即使我将指向另一个视图的约束更改为parent,但仍然不可见@yugansh
    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多