developer tip

레이아웃 XML 파일에 포함을 사용할 때 ID를 지정하는 방법

optionbox 2020. 8. 2. 18:24
반응형

레이아웃 XML 파일에 포함을 사용할 때 ID를 지정하는 방법


내 레이아웃 xml 파일에서 다른 레이아웃 xml 파일 (각각 다른 안드로이드 ID를 가짐)을 포함 시켰습니다.

<include layout="@layout/view_contact_name" android:id="+id/test1"/>
<include layout="@layout/view_contact_name" android:id="+id/test2"/>

내가 에뮬레이터에서 실행하고, 계층 구조 뷰어, 레이아웃 여전히 쇼 'NO_ID'의 각을 시작하고, 내 코드에 때, 나는이 findViewById(R.id.test1)findViewById(R.id.test2)모두 NULL을 반환합니다.

누구든지 내 문제를 도와주세요.


에 ID를 지정하십시오 <include>

<include layout="@layout/test" android:id="@+id/test1" />

그런 다음 두 개 findViewById사용 하여 레이아웃의 필드에 액세스하십시오.

View test1View = findViewById(R.id.test1);
TextView test1TextView = (TextView) test1View.findViewById(R.id.text);

이 방법을 사용하면 포함 된 모든 필드에 액세스 할 수 있습니다.


<merge>포함 레이아웃에서 태그를 사용하는 경우 포함의 ID가 실제보기가 아닌 병합 태그로 전송됩니다.

따라서 병합을 제거하거나 일부 레이아웃으로 바꾸십시오.

Tor Norbye 다음과 같이 썼습니다 .

<include>태그는 실제 볼 수 없습니다, 그래서 findByView 그것을 찾을 수 없습니다. @id 속성 (및 include 태그에 설정 한 다른 속성)은 포함 된 레이아웃의 루트 태그에 대신 적용됩니다. 따라서 activity.getView (R.id.included1)는 실제로 <TextView>그 자체 여야 합니다.


Romain Guy 태그 android:id안에 속성 을 넣어 포함 된 레이아웃의 ID를 재정의 할 수 있음나타냅니다<include> .

<include android:id="@+id/cell1" layout="@layout/workspace_screen" />

상단 답변이 가장 중요한 요점을 놓치고 <include/>태그를 포함 콘텐츠를 보유하는보기를 생성한다고 사람들을 오도 할 수 있다고 생각합니다 .

요점은 include의 idinclude의 레이아웃 파일의 루트 뷰로 전달 된다는 것입니다.

이것이 의미하는 것은 :

// activity_main.xml
<include layout="@layout/somelayout" android:id="@+id/someid"/>

// somelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

이것이된다 :

// activity_main.xml
<ImageView
    android:id="@+id/someid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

예는 이와 같지만 포함 필드에 삽입 된 레이아웃이 사용자 정의이고 해당 루트 레이아웃에 액세스하려는 경우주의하십시오. 이 경우 @ layout / test test의 레이아웃은 실제로 첫 번째 줄에 반환됩니다.

test test1View = (test)findViewById(R.id.test1);

문제는 현재 레이아웃 파일에서 선언되지 않은 id를 사용하려고한다는 것입니다. 다시 선언하는 대신 id를 사용하여 간단히 참조 할 수 있습니다 @+id/. Android Studio를 통해 원래 ID 이름을 리팩터링하면 포함 된 레이아웃에서도 리팩터링됩니다.

<include layout="@layout/toolbar"/>

<TextView
    android:id="@+id/txt_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    **android:layout_below="@+id/toolbar"**
    android:layout_marginTop="16dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"/>

  1. 포함 태그마다 ID를 설정해야합니다.
  2. 포함 된 하위 요소가 새 ID를 설정합니다. 새 ID를 생성하는 방법을 보려면 https://stackoverflow.com/a/15442898/1136117 항목을 확인하십시오.

inflated view의 인스턴스를 사용하여 <RecyclerView>id 찾으면 null<include> 을 반환 합니다.

public class ViewHolder extends RecyclerView.ViewHolder {

        private mTextView;

        public ViewHolder(View view) {
            super(view);
            View include_1 = view.findViewById(R.id.include_1);
            mTextView = (TextView) include_1.findViewById(R.id.text_id);
        }
    }

When talking about include you either have an id on the root view inside the included layout file or on the include line itself and not on both. For example:

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

Layout 1 file

<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout2">

</RelativeLayout>

The above example is wrong because technically you have two id's declared for the same layout. So what you have to do is pick which element will have the id.

참고URL : https://stackoverflow.com/questions/1759099/how-to-specify-id-when-uses-include-in-layout-xml-file

반응형