2013-10-06 3 views
1

켜기로 설정하면 아래에 LinearLayout이 확장되는 Android 스위치가 있습니다. 이를 위해 저는 애니메이션을 사용합니다. 화면을 시작할 때 버튼은 꺼져 있지만 LinearLayout이 표시됩니다. 이제 스위치를 켜고 다시 켤 때 실제로 LinearLayout이 숨겨집니다.하지만 제가 말한 것처럼 화면이 시작될 때마다 LinearLayout이 기본적으로 표시됩니다. 아무도 내가 화면을 시작할 때 기본적으로 LinearLayout을 숨길 수있는 방법을 알고 있습니까? 다음과 같이onCreate에서 Android LinearLayout을 숨기는 방법?

내가 지금 가지고있는 코드는 다음과 같습니다

public class MyClass extends Activity implements OnCheckedChangeListener { 
    Switch mySwitch; 

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

     mySwitch = (Switch) findViewById(R.id.my_switch); 
     mySwitch.setOnCheckedChangeListener(this); 
    } 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked){ 
      LinearLayout view = (LinearLayout) findViewById(R.id.view_to_expand); 
      Animation anim = expand(view, true); 
      view.startAnimation(anim); 
     } 
     else { 
      LinearLayout view = (LinearLayout) findViewById(R.id.view_to_expand); 
      Animation anim = expand(view, false); 
      view.startAnimation(anim); 
     } 
    } 

    public static Animation expand(final View v, final boolean expand) { 
     try { 
      Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class); 
      m.setAccessible(true); 
      m.invoke(v, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(((View) v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     final int initialHeight = v.getMeasuredHeight(); 
     if (expand) { 
      v.getLayoutParams().height = 0; 
     } else { 
      v.getLayoutParams().height = initialHeight; 
     } 
     v.setVisibility(View.VISIBLE); 
     Animation a = new Animation() { 
      @Override 
      protected void applyTransformation(float interpolatedTime, 
        Transformation t) { 
       int newHeight = 0; 
       if (expand) { 
        newHeight = (int) (initialHeight * interpolatedTime); 
       } else { 
        newHeight = (int) (initialHeight * (1 - interpolatedTime)); 
       } 
       v.getLayoutParams().height = newHeight; 
       v.requestLayout(); 
       if (interpolatedTime == 1 && !expand) 
        v.setVisibility(View.GONE); 
      } 

      @Override 
      public boolean willChangeBounds() { 
       return true; 
      } 
     }; 
     a.setDuration(250); 
     return a; 
    } 
} 
+0

레이아웃 XML을 보여주십시오에서 다른 하나

<ViewSwitcher xmlns:android = "http://schemas.android.com/apk/res/android" android:id="@+id/layoutSwitcher" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout></LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="6dip" > <ImageView.... </<LinearLayout> </ViewSwitcher> 

로 전환합니다. LL는 처음에 어떤 가시성 상태입니까? – Simon

+1

왜'layout_height'을 0으로 지정하지 않고 XML에서'가시성'을'사라지게 '할까요? –

답변

2

당신이 ViewSwitcher를 사용하려고 할 수 있습니다. XML 레이아웃에 추가하고 LinearLayout을 빈 레이아웃에 추가하십시오. 응용 프로그램에서 당신은 빈 레이아웃을 시작할 수 있습니다 시작하고 다음 코드

switcher = (ViewSwitcher) findViewById(R.id.layoutSwitcher); 
switcher.showNext(); 
관련 문제