2011-11-03 4 views
1

런타임에 동일한 버튼 id (이 경우 @ + id/button1)를 사용하는 레이아웃이 여러 개 있습니다. 레이아웃을 확장하고 주어진보기에서 각 버튼을 개별적으로 가져옵니다. 첫 번째 버튼은 잘 잡히지 만 findViewById (소유 뷰가 아니라 액티비티에서 호출 됨)를 사용하여 동일한 지정된 ID의 모든 후속 버튼을 찾을 수 없습니다.위젯이 ID 매개 변수를 사용하지 않습니다.

디버거에서 버튼을 검사하면 후속 버튼의 ID 태그가 거의 동일하지만 1 씩 증가합니다. ID의 인스턴스가 미리 존재하면 안드로이드가 XML 파일에서 제공 한 ID를 준수하지 않는 것 같습니다. 이 경우인가요? 그렇다면 뷰를 가로 질러 버튼을 어떻게 묶을 수 있습니까? 그렇다면 모든 위젯에 전역으로 고유 한 ID를 부여해야합니까?

+0

무슨 일인지 모르겠지만 레이아웃을 삭제하고 다시 빌드하면 문제가 해결 된 것 같습니다. 원래 문제는 사실이지만 여러 번 디버거를 밟았습니다. 자원에 대해 문제가 발생하고 XML 파일과 배포 된 리소스 사이의 위젯에 일치하지 않는 ID를 발생시키는 Android 프로젝트 용으로 컴파일되는 방법이 있습니다. – watcher278

답변

0

결국 각 버튼은 고유 ID를 호출 할 수있는 자체 static final int을 얻습니다. 맞습니까?

예, 각 버튼마다 고유 한 ID를 지정해야합니다 ... 그리고 button1, button2 등의 이름을 지정하면 안됩니다.

2

같은 뷰 계층 구조에 동시에 존재하는 모든 위젯 은 고유 한 ID 값을 가져야합니다 (). 즉, 응용 프로그램 레이아웃에서 @+id/button1을 재사용하는 것을 환영하지만 동일한 ID로 동일한 계층 구조로 여러보기를 팽창 시키면 모호한 결과를 초래할 수 있습니다.

실제 레이아웃이 어떻게 구성되는지에 따라 다르지만 일부 모호성을 해결하기 위해 할 수있는 또 다른 방법은 계층 구조를 내려가는 다른 뷰에서 findViewById()를 호출하는 것입니다. 예를 들어 다음과 같이 단일 레이아웃을 만들 수 있습니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
    android:id="@+id/row_one" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Row One"/> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    </LinearLayout> 
    <LinearLayout 
    android:id="@+id/row_two" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Row Two"/> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    </LinearLayout> 
    <LinearLayout 
    android:id="@+id/row_three" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Row Three"/> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    </LinearLayout> 
</LinearLayout> 

모든 버튼의 ID 값이 같은지 확인하십시오. 이 버튼에 대한 참조를 얻기 위해 내 활동에서 findViewById()으로 전화 할 수는 없습니다 ... 어느 것을 얻을 수 있습니까 ?? 그러나 findViewById()은 임의의보기에서 호출 할 수 있습니다, 그래서 각 버튼에 대한 참조를 얻기 위해 다음을 수행 할 수

setContentView(R.layout.main); 

Button one = (Button)findViewById(R.id.row_one).findViewById(R.id.button); 
Button two = (Button)findViewById(R.id.row_two).findViewById(R.id.button); 
Button three = (Button)findViewById(R.id.row_three).findViewById(R.id.button); 

을 그리고 지금 나는 그들이 동일한 ID를했다하더라도, 각각의 고유 한 버튼에 대한 참조를 가지고있다. 귀하의 응용 프로그램이 예제와 일치하는 경우 나는 여전히 이것을지지한다고 말하지 않았습니다. 고유 ID 참조를 작성하면 코드를 읽을 수있게 유지하는 데 도움이됩니다.

HTH!

+0

감사합니다 Devunwired, 내가 viewViewById() 뷰 호출 당 활동을 호출합니다. 수동으로 내 견해를 관리하고 그 사이에 사용자 정의 전환을 수행하므로 시동시 버튼을 팽창시키고 바인딩합니다. 그렇기 때문에 안드로이드가 ID를 존중하지 않는 이유가 너무 당황 스러웠습니다. 전체 계획에서 각 버튼의 이름을 고유하게 지정하지 않으려 고했습니다. – watcher278

관련 문제