我有一个问题,我的应用程序中的菜单以一种略微奇怪的方式打开,我在下面放了一个截图来说明我的意思。正如您所看到的,菜单正在显示并将片段移开。我想要它,使它出现在现有片段的顶部。我用动画展示了这个。
动画代码如下:
public static void open(final Context context,final RelativeLayout layout){
Animation in = AnimationUtils.loadAnimation(context, R.anim.push_right_out_80);
layout.startAnimation(in);
in.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
layout.startAnimation(anim);
display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int left = (int) (display.getWidth() * 1.0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(layout.getLayoutParams());
params.setMargins(left / 2, 0, -left / 2, 0);
params.addRule(RelativeLayout.ABOVE, R.id.container);
layout.setLayoutParams(params);
}
});
}
这会调用anim文件夹中的xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromXDelta="0"
android:fromAlpha="0.0"
android:toAlpha="0.3"
android:toXDelta="+50%p"/>
</set>
菜单是listview,它位于activity_home.xml中,如下所示:
<RelativeLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
<ListView
android:id="@+id/listMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:listSelector="@android:color/darker_gray"/>
<RelativeLayout
android:id="@+id/fragmentspace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
</RelativeLayout>
</RelativeLayout>
菜单进入ListView,我的片段进入相对布局。
要关闭菜单我有这个代码(不太确定是否需要调整?)
public static void close(final Context context, final RelativeLayout layout){
Animation out = AnimationUtils.loadAnimation(context, R.anim.push_left_in);
layout.startAnimation(out);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.setMargins(0, 0, 0, 0);
layout.setLayoutParams(params);
}
close代码也会在anim文件夹中调用另一个xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromAlpha="0.3"
android:fromXDelta="+50%p"
android:toAlpha="0.0"
android:toXDelta="0"/>
</set>