How to include layout inside layout?

前端 未结 6 769
执念已碎
执念已碎 2020-12-12 20:24

How to include layout inside layout in Android?

I am creating common layout. I want to include that layout in another page.

相关标签:
6条回答
  • 2020-12-12 20:27

    Edit: As in a comment rightly requested here some more information. Use the include tag

    <include
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       layout="@layout/yourlayout" />
    

    to include the layout you want to reuse.

    Check this link out...

    0 讨论(0)
  • 2020-12-12 20:36

    Note that if you include android:id... into the <include /> tag, it will override whatever id was defined inside the included layout. For example:

    <include
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/some_id_if_needed"
       layout="@layout/yourlayout" />
    

    yourlayout.xml:

    <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/some_other_id">
       <Button
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:id="@+id/button1" />
     </LinearLayout>
    

    Then you would reference this included layout in code as follows:

    View includedLayout = findViewById(R.id.some_id_if_needed);
    Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);
    
    0 讨论(0)
  • 2020-12-12 20:36

    Try this

    <include
                android:id="@+id/OnlineOffline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                layout="@layout/YourLayoutName" />
    
    0 讨论(0)
  • 2020-12-12 20:41

    Use <include /> tag.

              <include 
                android:id="@+id/some_id_if_needed"
                layout="@layout/some_layout"/>
    

    Also, read Creating Reusable UI Components and Merging Layouts articles.

    0 讨论(0)
  • 2020-12-12 20:50

    Learn More Using this link https://developer.android.com/training/improving-layouts/reusing-layouts.html

        <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".Game_logic">
        
              
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
        
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:id="@+id/text1"
                        android:textStyle="bold"
                        tools:text="Player " />
        
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"
                        android:layout_marginLeft="20dp"
        
                        android:id="@+id/text2"
                        tools:text="Player 2" />
              
                
              
            </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    

    Blockquote

    • Above layout you can used in other activity using

       <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:app="http://schemas.android.com/apk/res-auto"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        tools:context=".SinglePlayer">   
      
                <include layout="@layout/activity_game_logic"/> 
            </androidx.constraintlayout.widget.ConstraintLayout>
      
    0 讨论(0)
  • 2020-12-12 20:54

    From Official documents about Re-using Layouts

    Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the tag to embed another layout inside the current layout.

    Here is my header.xml file which i can reuse using include tag

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        >
    
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="#000000" />
    
    </RelativeLayout>
    

    No I use the tag in XML to add another layout from another XML file.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#f0f0f0" >
    
    
        <include
            android:id="@+id/header_VIEW"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/header" />
    
            <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#ffffff"
            android:orientation="vertical"
            android:padding="5dp" >
    
    
        </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题