我想膨胀一个将成为“无网络对话框”的视图。我创建了一个自定义布局,现在我想对其进行充气和显示。我遇到的冲突是动画不起作用。我希望对话框从屏幕底部滑到中间,然后在最后反弹。这是我正在使用的。
styles.xml
<!-- animation for network dialog -->
<style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">@anim/slide_up_bounce</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
这是我的slide_up_bounce.xml
<scale
android:duration="600"
android:fromXScale="1"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="600"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<translate
android:duration="600"
android:fromYDelta="50%"
android:toYDelta="0%" />
在Utility类中,我有一个看起来像
public static void networkDialog(Context context) {
final Dialog dialog = new Dialog(context, R.style.DialogAnimation);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_network, null);
dialog.setContentView(view);
ImageButton ibRefresh = (ImageButton) view.findViewById(R.id.ib_refresh);
}
此刻,对话框立即显示,没有滑动或弹跳。我该如何解决这个问题,还是应该尝试采用其他方法来实现这一目标?提前致谢!
最佳答案
我想出了怎么做。与使用Dialog相比,最好使用DialogFragment。无论出于何种原因,我的Dialog实现都是正确的,但不适用于动画。这是我的实现:
public class NetworkDialog extends DialogFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.dialog_network, container, false);
v.setAlpha(0.5f);
final ImageButton ibRefresh = (ImageButton) v.findViewById(R.id.ib_refresh);
ibRefresh.setClickable(true);
ibRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapActivity.rotateImageView(ibRefresh);
}
});
return v;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
}
然后我用
NetworkDialog dialogFragment = new NetworkDialog();
dialogFragment.show(getFragmentManager(), "ProgressDialog");