Development Tip

레이아웃 내부에 레이아웃을 포함하는 방법은 무엇입니까?

yourdevel 2020. 10. 8. 19:07
반응형

레이아웃 내부에 레이아웃을 포함하는 방법은 무엇입니까?


Android에서 레이아웃 내부에 레이아웃을 포함하는 방법은 무엇입니까?

공통 레이아웃을 만들고 있습니다. 해당 레이아웃을 다른 페이지에 포함하고 싶습니다.


편집 : 여기에 더 많은 정보를 요청한 의견에서와 같이. include태그 사용

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

재사용하려는 레이아웃을 포함합니다.

이 링크를 확인하십시오 ...


당신이 포함 할 경우주의 android:id...<include />태그가 포함 된 레이아웃 내부에 정의 된 어떤 ID를 우선합니다. 예를 들면 :

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

그런 다음 코드에서이 포함 된 레이아웃을 다음과 같이 참조합니다.

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

<include />태그를 사용하십시오 .

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

또한 재사용 가능한 UI 구성 요소 만들기레이아웃 병합 문서를 읽어보세요.


이 시도

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

레이아웃 재사용 에 대한 공식 문서에서

Android는 작고 재사용 가능한 대화 형 요소를 제공하기 위해 다양한 위젯을 제공하지만 특수 레이아웃이 필요한 더 큰 구성 요소를 재사용해야 할 수도 있습니다. 전체 레이아웃을 효율적으로 재사용하려면 태그를 사용하여 현재 레이아웃 안에 다른 레이아웃을 포함 할 수 있습니다.

다음은 include 태그를 사용하여 재사용 할 수있는 header.xml 파일입니다.

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

아니 나는 사용 다른 XML 파일에서 다른 레이아웃을 추가하려면 XML의 태그를 사용하십시오.

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

참고 URL : https://stackoverflow.com/questions/5632114/how-to-include-layout-inside-layout

반응형