Second layout is not showing with include tag in a

2019-06-20 19:41发布

I have designed two Android layouts which should be shown in order. So, the first layout must be showing in top of the page and the second layout must be showing in bottom of the page. However, the following code only shows the first layout. How can I show the second layout too?

the main layout is

<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>

The first xml layout is

<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>

The second layout is

<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>

3条回答
The star\"
2楼-- · 2019-06-20 20:26

I'm not yet sure if this will solve your problem, but can you try adding android:layout_width and android:layout_height in your included layout as well as layout weight?

<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"/>

Added info

The reason why you have that problem is because both your layouts uses match_parent for the height. That means that at the first layout, it will match the size of the parent (which in this case is the whole screen), thus, the first layout will occupy the whole screen and the second layout won't appear.

In this solution, you are telling both layouts to consume only half of the screen by using the layout_weight.

查看更多
Melony?
3楼-- · 2019-06-20 20:28

You should use RelativeLayout on your main layout. If you use LinearLayout one item will always be shown bellow the previous element and since both yours layout are match_parent the second won't be shown.

查看更多
Viruses.
4楼-- · 2019-06-20 20:39

In your first layout, you have

android:layout_height="match_parent"

so it is taking up the entire height of your main layout. Change that to

android:layout_height="wrap_content"

and you should be good.

From the docs

The root View should be exactly how you'd like it to appear in each layout to which you add this layout.

Note that

You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the <include/> tag.

查看更多
登录 后发表回答