2012-04-10 4 views
1

다른 b.xml 레이아웃에 포함 된 a.xml 레이아웃에있는보기에 액세스해야합니다. 예를 들어 , 이 내가 자바에서 할 경우는 있어야하기 때문에 후, XML 코드에서 그것을 할 필요가포함 된 XML 레이아웃에 대한 액세스보기

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <include 
     android:id="@+id/a_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     layout="@layout/a" /> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/xyz" 
     android:text="Show me below xyz" /> 
</RelativeLayout> 

b.xml에,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/xyz" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="XYZ" /> 
</RelativeLayout> 

a.xml이며, setContentView()를 실행하고 TextView 'Label'의 LayoutParams를 설정하면 효과가 없습니다.

누구나 내가 물어 보려는 것을 이해합니다. 좋은 응답을 기다리고 있습니다.

감사합니다.

오른쪽의 이미지는 현재 코드에서 얻고 자하는 것입니다.

android:layout_below="@id/xyz" 

android:layout_below="@id/a_layout" 

에 그리고 당신은 내가에 배치 여기 (코드에서 사용할 수 있습니다 :

This is what I am getting with the current xml code This is what I am trying to do

+0

물어하려고 무엇 :이 a + b 레이아웃 조합과 같은 동작을 관찰 할 수있다? –

+0

내가 짐작했다면, id :'findViewById (R.id.label)' –

+0

에 액세스하십시오. 맞습니다. findViewById (R.id.label)를 사용하여 액세스 할 수 있습니다. 하지만이 안드로이드를 할 수 없어 : layout_below = "@ id/xyz" –

답변

3

b.xml에서, 당신은 수정해야합니다 onCreate) :

setContentView(R.layout.b);  
((Button)findViewById(R.id.xyz)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ((TextView)findViewById(R.id.label)).setText("clicked on XYZ button"); 
     } 
    }); 
+0

이것은 b.xml 포함 레이아웃에서 볼 수 없었던 android : layout_height = "fill_parent"이므로 TextView가 표시되지 않습니다. –

+0

@Trickster 아, 다시 확인해 주셔서 깜빡 했군요 - 안드로이드가 있어야합니다 : layout_height = "wrap_content"고마워요. –

0

문제는 포함 된 레이아웃의 View에 액세스하는 것이 아니라이 레이아웃을 "중첩"할 수 없다는 점에서 문제가됩니다. 설명해 드리겠습니다 : a.xml의 버튼 아래에 뷰를 더 추가 한 다음 버튼 바로 아래에있는 b.xml에 일부 뷰를 배치하면 b.xml의 뷰가 a.xml에서 겹치게됩니다. 그러나 이것은 구현되지 않았습니다. 안드로이드 (아직?). 그래서 당신이 할 수있는 유일한 일은 @ Hoàng Toản이 제시 한대로 android:layout_below="@id/a_layout"을 넣는 것입니다.

P.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/xyz" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="XYZ" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/xyz" 
     android:text="Show me below xyz" /> 
</RelativeLayout> 
+0

사실, 내 애플 리케이션의 레이아웃은 예제와 매우 다르다. 그래서 그 둘을 결합 할 수 없다. 예를 들어 이것들은 단지 두 개의 레이아웃 일뿐입니다. 그러나 내 프로젝트에는 a.xml을 포함하는 15 개의 레이아웃이 있고 a.xml은 예제와 같이 단순하지 않습니다. 이것은 각 활동의 헤더입니다. –

+0

필자가 지적했듯이, 현재의 Android SDK로는 정교한 구조를 얻을 수 없습니다.'a' +'b' 조합은 그것을 보여줍니다 ('include' 요소는 없지만 여전히 동일한 동작을 보입니까?). –

관련 문제