2014-11-20 5 views
16

내 응용 프로그램에 하나의 레이아웃을 포함해야합니다. 그래서 내가 사용했습니다Android에 프로그래밍 방식으로 레이아웃을 숨기려면 어떻게해야합니까?

<include 
    android:id="@+id/support_layout" 
    android:width="match_parent" 
    android:height="match_parent" 
    layout="@layout/support"/> 

나는이 자바 스크립트 파일에 포함 된 태그를 View를 사용하여 참조했습니다.

View v = (View) findViewById(R.id.support_layout); 

하지만 내 코드의 일부 지점에서이 레이아웃을 숨겨야합니다. 그래서 내가 사용했다 v.GONE

그러나 그것은 은신처가 아니다. 프로그래밍 방식으로 XML에있는 텍스트 및 단추 특성을 참조하고 싶습니다. 어떻게 할 수 있습니까? 우리는 당신이 언급 한 View 것을 숨어의 실제 구현을 볼 필요가

<LinearLayout 
    android:id="@+id/support_layout" 
    android:width="match_parent" 
    android:height="match_parent"> 

    <TextView 
     android:id="@+id/txt" 
     android:width="match_parent" 
     android:height="wrap_content"/> 

    <Button 
     android:id="@+id/btn" 
     android:width="match_parent" 
     android:height="wrap_content" 
     android:text="Button"/> 

</LinearLayout> 
+0

게시 할 수 있습니까? – ridsatrio

+1

사용하려고 시도합니다. v.setVisibility (View.GONE); 대신 v.GONE 만 사용하십시오. –

답변

12

:

내 support.xml 있습니다.

그러나 질문을 읽는 즉시, 나는 잘못된 방식으로 생각할 수도 있습니다.

이 사용 숨기거나보기가 보이지 않게하려면이 compeletly보기를 제거하지 않는다는 점을 염두에

yourView.setVisibility(View.INVISIBLE); 

곰; 그것은 당신의 레이아웃에 여전히 남아있을 것이고 그것에 대한 참조를 얻거나 그것을 조작하려고 시도 할 수 있습니다.

는 compeletly 제거 대신를 사용하려면

yourView.setVisibility(View.GONE); 

을 이제 당신이 호출 할 경우, yourView가 compeletly 레이아웃에서 제거 될 것이다. 더 이상 참조 할 수 없습니다.

+0

View.Visible 사용자가 다시이 레이아웃을 볼 수있게 되나요? –

+0

@DharmasaiSeerapu, 물론. 'yourView.setVisibility (View.VISIBLE) '를 호출하면,'View '가 다시 보일 것입니다. – ridsatrio

+0

하지만 내 경우에는 레이아웃의 요소에 액세스 할 때 오류가 표시됩니다. –

2

할 수 있습니다 setVisibility()를 호출이 "포함"레이아웃 숨기기 :

v.setVisibility(View.GONE) 

을하고 전화로 나중에 보여

v.setVisibility(View.VISIBLE) 

은 findViewById를 방법을 사용할 수 있습니다 지원 레이아웃에서 버튼과 텍스트 뷰를 참조하려면 포함 된보기 (잘 모르겠지만 필수 항목이 아니라고 생각하면 직접 활동보기에서 호출 할 수 있습니다) :

View supportLayout = (View) findViewById(R.id.support_layout); 
Textview txv = (TextView) findViewById(R.id.txt); 
Button btn = (Button) findViewById(R.id.btn); 

(이 작동하지 않는 경우 시도와 : 버튼 BTN = (버튼) supportLayout.findViewById (R.id.btn))

- 참고 -

당신이 태그를 포함하는 attributs을 제공

그것 override ones of the included layout (there support_layout LinearLayout) 그렇게하지 않아도됩니다.

4

이 뷰를 선형 레이아웃에 넣고 선형 레이아웃을 숨 깁니다. 효과가있을 것입니다.

<LinearLayout 
android:id="@+id/support_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<include 
    layout="@layout/support" 
    android:height="match_parent" 
    android:width="match_parent" /> </LinearLayout> 

View 대신 Linearlayout을 쓰는 것을 잊지 마십시오.대신

View v = (View) findViewById(R.id.support_layout); 

간단히이 당신이`것을 View`를 숨기기 위해 사용하는 코드를

LinearLayout v = (LinearLayout) findViewById(R.id.support_layout); 
+0

include 태그를 사용하는 것을 무효화합니다.이 개념은 계층 구조를 평평하게 만드는 것입니다. – goonerDroid

관련 문제