1

我已经被这个问题困扰了2天,试图自己解决。我希望我的应用程序有导航抽屉,片段对我来说很难理解。我不想更改我的任何代码。我看过这个例子,但它并没有帮助我理解太多。

我只是不知道如何在没有抽屉的情况下实现导航。

任何帮助表示赞赏。

谢谢。

4

2 回答 2

2

使用此代码,您可以执行任何滑动动画菜单布局是左侧的一个幻灯片,实际布局是您的布局首先对用户可见。总布局包含两者。我曾经遇到过同样的问题,所以我完成了这段代码,它工作得很好

一切顺利 :)

MainActivity.java 的代码

package com.example.slidemenuanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements AnimationListener {

    int ActualLayoutWidth, MenuLayoutWidth, TotalLayoutWidth;
    RelativeLayout actualLayout, totalLayout;
    LinearLayout menuLayout;
    RelativeLayout.LayoutParams menuLayoutParams, actualLayoutParams;
    FrameLayout.LayoutParams totallayoutParams;

    ImageView menulayoutImage, actualLayoutImage;

    TranslateAnimation MenuClickRightAnim, MenuClickLeftAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Display display = getWindowManager().getDefaultDisplay();
        ActualLayoutWidth = display.getWidth();
        MenuLayoutWidth = display.getWidth();
        TotalLayoutWidth = ActualLayoutWidth + MenuLayoutWidth;

        menuLayout = (LinearLayout) findViewById(R.id.menu_layout);
        actualLayout = (RelativeLayout) findViewById(R.id.actuallayout);
        totalLayout = (RelativeLayout) findViewById(R.id.totallayout);

        // parmeters for total layout
        totallayoutParams = new FrameLayout.LayoutParams(TotalLayoutWidth,
                FrameLayout.LayoutParams.FILL_PARENT, 3);
        totallayoutParams.setMargins(-MenuLayoutWidth, 0, 0, 0);
        totalLayout.setLayoutParams(totallayoutParams);

        // parmeters for menu layout
        menuLayoutParams = new RelativeLayout.LayoutParams(MenuLayoutWidth,
                RelativeLayout.LayoutParams.FILL_PARENT);
        menuLayout.setLayoutParams(menuLayoutParams);

        // parameters for actual layout
        actualLayoutParams = new RelativeLayout.LayoutParams(ActualLayoutWidth,
                RelativeLayout.LayoutParams.FILL_PARENT);
        actualLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.menu_layout);
        actualLayout.setLayoutParams(actualLayoutParams);

        // intializing animations
        // menu moves right
        MenuClickRightAnim = new TranslateAnimation(0, MenuLayoutWidth, 0, 0);
        MenuClickRightAnim.setFillAfter(true);
        MenuClickRightAnim.setAnimationListener(this);
        MenuClickRightAnim.setDuration(750);

        // menu moves left
        MenuClickLeftAnim = new TranslateAnimation(0, -MenuLayoutWidth, 0, 0);
        MenuClickLeftAnim.setAnimationListener(this);
        MenuClickLeftAnim.setFillAfter(true);
        MenuClickLeftAnim.setDuration(750);

        // init widgets
        menulayoutImage = (ImageView) findViewById(R.id.imagein_menulayout);

        menulayoutImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                totalLayout.setAnimation(MenuClickLeftAnim);
                totalLayout.startAnimation(MenuClickLeftAnim);
            }
        });

        actualLayoutImage = (ImageView) findViewById(R.id.imagein_actuallayout);

        actualLayoutImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                totalLayout.setAnimation(MenuClickRightAnim);
                totalLayout.startAnimation(MenuClickRightAnim);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onAnimationEnd(Animation animation) {

        if (animation == MenuClickRightAnim) {

            // setting params for total layout because naimtion transfers raw
            // pizels not actual layout
            totalLayout.clearAnimation();
            totallayoutParams = new FrameLayout.LayoutParams(TotalLayoutWidth,
                    FrameLayout.LayoutParams.FILL_PARENT, 3);
            totallayoutParams.setMargins(0, 0, 0, 0);
            totalLayout.setLayoutParams(totallayoutParams);

        }
        if (animation == MenuClickLeftAnim) {

            totalLayout.clearAnimation();
            totallayoutParams = new FrameLayout.LayoutParams(TotalLayoutWidth,
                    FrameLayout.LayoutParams.FILL_PARENT, 3);
            totallayoutParams.setMargins(-MenuLayoutWidth, 0, 0, 0);
            totalLayout.setLayoutParams(totallayoutParams);

        }

    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub

    }

}

xml activitymain.xml 的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/totallayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F6F6F6"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/menu_layout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imagein_menulayout"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/actuallayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imagein_actuallayout"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>

</RelativeLayout>
于 2013-08-27T03:48:24.067 回答
1

我也有这个问题。如果您的问题是有多个片段,您可以修复它,但这并不理想。您需要做的是进行多项活动,并且在每一项活动中您都必须重新创建导航抽屉。但无论如何,您仍然必须将视图设置为片段。

但是..如果您想使用 NO 片段,唯一的方法是为每个 xml 布局添加抽屉布局(导航抽屉只是屏幕侧面的抽屉布局)。这是人们在谷歌推出自己的导航抽屉之前所做的......

我知道这可能不是您正在寻找的答案,但我希望它有所帮助

于 2013-08-27T03:38:06.730 回答