Làm thế nào để bao gồm bố cục bên trong bố cục trong Android?
Tôi đang tạo bố cục chung. Tôi muốn đưa bố cục đó vào một trang khác.
Câu trả lời:
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 ...
<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ó.
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);
Sử dụng <include />thẻ.
<include
android:id="@+id/some_id_if_needed"
layout="@layout/some_layout"/>
Ngoài ra, hãy đọc bài viết Tạo các thành phần giao diện người dùng có thể sử dụng lại và hợp nhất các bố cục .
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>
Because I want to reuse a ProgressBarvấn đề gì khác sắp tới không?
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>