【问题标题】:Second layout is not showing with include tag in android studioandroid studio中的第二个布局未显示包含标签
【发布时间】:2019-06-27 07:16:46
【问题描述】:

我设计了两个应按顺序显示的 Android 布局。因此,第一个布局必须显示在页面顶部,第二个布局必须显示在页面底部。但是,以下代码仅显示第一个布局。如何也显示第二个布局?

主要布局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/first_layout"/>
    <include layout="@layout/second_layout"/>

</LinearLayout>

第一个xml布局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="net.simplifiedcoding.androidloginapp.UserProfile">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView3"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</LinearLayout>

第二个布局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"       
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"      
    tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:id="@+id/textView" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextName" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Address"
    android:id="@+id/textView2" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextAddress" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Insert"
    android:onClick="insert"
    android:id="@+id/button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textViewResult" />
</LinearLayout>

【问题讨论】:

  • 在预览中,主布局和第一个布局在屏幕上占用了多少空间?
  • 显示您用于显示第一个布局的 Java 代码

标签: android android-layout android-studio android-xml


【解决方案1】:

我还不确定这是否能解决您的问题,但您可以尝试在包含的布局中添加 android:layout_widthandroid:layout_height 以及布局权重吗?

<include layout="@layout/first_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<include layout="@layout/second_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

添加信息

你有这个问题的原因是因为你的两个布局都使用match_parent 作为高度。这意味着在第一个布局中,它将匹配父级的大小(在这种情况下是整个屏幕),因此,第一个布局将占据整个屏幕,而第二个布局不会出现。

在此解决方案中,您通过使用 layout_weight 告诉两种布局只占用屏幕的一半。

【讨论】:

  • 是的,它正在工作,但你能添加一些解释它是如何解决的吗?
  • @MohammadAliNematollahi 顺便说一句,您尝试过 codeMagic 的解决方案吗?我想知道这是否可行。
  • 我现在也被否决了:/ 但我不明白它有什么问题
  • @codeMagic 我稍后会尝试,如果您的解决方案有效,我会支持您。哈哈
  • 我只是按照你说的改变了 android:layout_height 但它没有用
【解决方案2】:

在你的第一个布局中,你有

android:layout_height="match_parent"

所以它占据了主布局的整个高度。将其更改为

android:layout_height="wrap_content"

你应该很好。

From the docs

根视图应该正是您希望它在您添加此布局的每个布局中出现的方式。

注意

您还可以通过在 &lt;include/&gt; 标记中指定它们来覆盖包含布局的根视图的所有布局参数(任何 android:layout_* 属性)。

【讨论】:

    【解决方案3】:

    您应该在主布局上使用 RelativeLayout。如果您使用 LinearLayout 一个项目将始终显示在前一个元素的下方,并且由于您的两个布局都是 match_parent ,第二个将不会显示。

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多