2014-01-27 4 views
0

toggleButton을 사용하여 imageView를 숨기거나 표시하려고하지만 작동하지 않습니다. 다음은 관련 부품의 내 코드입니다 :
XML :Android - ToggleButton이있는 imageView 표시/숨기기

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

    <Switch 
    android:id="@+id/toggleButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:textOff="auto" 
    android:textOn="manuel" /> 

    <ImageView 
    android:id="@+id/compass" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/compass" /> 

    <ImageView 
    android:id="@+id/compass_direction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/compass_direction" 
    android:rotation="180" /> 

</RelativeLayout> 

그리고 여기 JAVA 부분 :

public class SnowActivity extends MenuActivity implements SensorEventListener, 
    OnCheckedChangeListener, OnTouchListener, ScrollViewListener { 

    private Switch toggleButton; 
    private ImageView compass; 
    private ImageView compass_direction; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_snow); 

    toggleButton = (Switch) findViewById(R.id.toggleButton); 
    toggleButton.setOnCheckedChangeListener(this); 

    compass = (ImageView) findViewById(R.id.compass); 
    compass_direction = (ImageView) findViewById(R.id.compass_direction); 
    } 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    sensorEnabled = !isChecked; 
    if (sensorEnabled) { 
     compass.setVisibility(View.VISIBLE); 
     compass_direction.setVisibility(View.VISIBLE); 
    } else { 
     compass.setVisibility(View.INVISIBLE); 
     compass_direction.setVisibility(View.INVISIBLE); 
    } 
} 
} 

나는 수동으로

와 XML의 이미지 뷰에 보이는 태그를 추가하는 경우
android:visibility = "visible" 

최소한 사진은 '그래픽 레이아웃'에 숨겨져 있지만 프로그래밍 방식으로는 표시되지 않습니다. 게다가 onCheckedChange() 메서드에서 호출되는 추가 메서드가 있으므로 ToggleButton이 올바르게 구현 된 것 같습니다.

미리 감사드립니다. 인사말.

답변

0

xml 레이아웃의 컨텍스트 특성을 .MainActivity 대신 .SnowActivity로 설정하십시오.

+0

'tools' 속성은 런타임시 영향을주지 않습니다. – laalto

+0

시도했지만 작동하지 않았습니다. –

+0

좋아, 내 잘못이야. 디버깅 할 경우 onCheckedChanged (Log.i ("SnowActivity", "SensorEnabled flag is"+ Boolean.toString (sensorEnabled));) 안에 sensorEnabled 값을 기록합니다. – Lazarus