Làm thế nào để bao gồm bố cục bên trong bố cục?


Câu trả lời:


193

Chỉnh sửa: Như trong một bình luận đúng yêu cầu ở đây một số thông tin thêm. Sử dụng includethẻ

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

để bao gồm bố cục bạn muốn sử dụng lại.

Kiểm tra liên kết này ra ...


11
chỉ là một chi tiết nhỏ: sử dụng android: layout_width = "match_parent" thay vì android: layout_width = "fill_parent". fill_parent không được dùng nữa.
Trinity

1
Tôi có thể bao gồm một bố cục và đặt một số thuộc tính của nó thông qua xml, ví dụ: đặt một chuỗi văn bản trong lớp con trực tiếp trong thẻ <include> không?
JohnyTex

@JohnyTex Không chắc bạn có thể thực hiện trực tiếp trong <include />thẻ hay không, tuy nhiên, bạn có thể thực hiện bằng cách sử dụng mã java. xem câu trả lời của Phileo99 bên dưới để biết cách tham chiếu đến bố cục bao gồm. và sau đó bạn có thể thay đổi nội dung của nó.
Moses

56

Lưu ý rằng nếu bạn đưa android:id...vào <include />thẻ, nó sẽ ghi đè bất kỳ id nào đã được xác định bên trong bố cục được bao gồm. Ví dụ:

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

Sau đó, bạn sẽ tham chiếu bố cục bao gồm này trong mã như sau:

View includedLayout = findViewById(R.id.some_id_if_needed);
Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);


6

Thử cái này

<include
            android:id="@+id/OnlineOffline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            layout="@layout/YourLayoutName" />

2

Từ tài liệu chính thức về việc sử dụng lại bố cục

Mặc dù Android cung cấp nhiều tiện ích con để cung cấp các yếu tố tương tác nhỏ và có thể tái sử dụng, bạn cũng có thể cần sử dụng lại các thành phần lớn hơn yêu cầu bố cục đặc biệt. Để sử dụng lại hiệu quả các bố cục hoàn chỉnh, bạn có thể sử dụng thẻ để nhúng một bố cục khác vào bên trong bố cục hiện tại.

Đây là tệp header.xml của tôi mà tôi có thể sử dụng lại bằng thẻ include

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

Không, tôi sử dụng trong XML để thêm một bố cục khác từ một tệp XML khác.

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

Tại sao lại đặt TextView trong RelativeLayout mà không phải là root view?
Florian Walther

@FlorianWalther Đây là một ví dụ
IntelliJ Amiya

Cảm ơn đã phản ứng nhanh chóng. Nhưng tôi có đúng khi đặt TextView làm phần tử gốc hay tôi đang thiếu thứ gì đó? Bởi vì tôi muốn sử dụng lại ProgressBar và tự hỏi liệu tôi có phải đặt nó vào một bố cục hay không.
Florian Walther

@FlorianCó Because I want to reuse a ProgressBarvấn đề gì khác sắp tới không?
IntelliJ Amiya

Không có vấn đề gì, nó hoạt động. Nhưng tất cả các ví dụ tôi thấy trên mạng đều đặt một widget duy nhất vào một bố cục khác và tôi tự hỏi tại sao.
Florian Walther

0

Tìm hiểu thêm Sử dụng liên kết này 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

  • Bố cục trên bạn có thể sử dụng trong hoạt động khác bằng cách sử dụng

     <?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>
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.