1

在我的应用程序中,当单击按钮时,我有一个从顶部移入的菜单。该菜单嵌套在 LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical">

    <!-- Top Menu -->

    <LinearLayout
        android:id="@+id/top_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:orientation="vertical">

        <The 3 Menu Buttons as ImageButton>
    </LinearLayout>

    <!-- End of Top Menu -->

    ...[Rest of Layout / the always visible content
</LinearLayout>

现在我希望在创建 Activity 时隐藏菜单,如下所示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_someactivity);        

    animHelp = new AnimationHelper(getWindow().getDecorView().getRootView());        
    // Not really working temporary fix for the Animation 'moveup thingy' that happens the first time (view.gone/view.visible issue)
    animHelp.doSlideUp(0); // int param = animation.setDuration(0);

问题:当活动开始时,您会看到动画发生/内容部分向上移动

问题:我怎样才能以看不到动画发生的方式隐藏 top_menu?

尝试: - 使用 Visibility:Gone 作为启动参数也不能解决问题。

为了完整起见,这里是 AnimationHelper.class

public class AnimationHelper {

    private boolean isDown = false;

    private LinearLayout ll;
    private LinearLayout menu;

    public AnimationHelper(View v) {
        ll = (LinearLayout)v.findViewById(R.id.root);
        menu  = (LinearLayout) v.findViewById(R.id.top_menu);
    }

    public void doSlideDown(int time) {
        menu.setVisibility(View.VISIBLE);

        Animation slideDown = setLayoutAnim_slidedown(time);
        ll.startAnimation(slideDown);
    }

    public void doSlideUp(int time) {

         Animation slideUp = setLayoutAnim_slideup(time);
         ll.startAnimation(slideUp);
    }

    private Animation setLayoutAnim_slidedown(int time) {

         Animation animation = new TranslateAnimation(
                 Animation.ABSOLUTE, 0,
                 Animation.ABSOLUTE, 0,
                 Animation.ABSOLUTE, -menu.getHeight(),
                 Animation.ABSOLUTE, 0);

            animation.setDuration(time);
            animation.setAnimationListener(new AnimationListener() {

                public void onAnimationStart(Animation animation) {}

                public void onAnimationEnd(Animation animation) {
                    isDown = true;              
                }

                public void onAnimationRepeat(Animation animation) {}

            });

         return animation;

    }

    private Animation setLayoutAnim_slideup(int time) {

        Animation animation = new TranslateAnimation(
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, -menu.getHeight());

        animation.setDuration(time);
        animation.setAnimationListener(new AnimationListener() {        

            public void onAnimationStart(Animation animation) {}

            public void onAnimationEnd(Animation animation) {
                ll.clearAnimation();
                menu.setVisibility(View.GONE);
                isDown = false;

            }

            public void onAnimationRepeat(Animation animation) {}

         });

         return animation;
    }

    public boolean isDown() {
        return isDown;
    }

}

非常感谢您的宝贵时间!

4

0 回答 0