我终于想出了一个(主要是复制粘贴代码)在应用程序中播放动画GIF的简单方法,以便我可以将它用作预加载器。我正在使用DialogFragment
,里面有WebView
。一切都很好,只是图像显示在屏幕的左上角,垂直居中。这里是我的代码里面DialogFragment
:位置中心的对话框片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_please_wait, container, false);
WebView gifPlayer = (WebView) view.findViewById(R.id.gifPlayer);
gifPlayer.loadUrl("file:///android_asset/img/preloader_7.gif");
gifPlayer.setBackgroundColor(0x00000000);
// http://stackoverflow.com/questions/9698410/position-of-dialogfragment-in-android
getDialog().getWindow().setGravity(Gravity.CENTER);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return view;
}
和我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/transparent"
android:orientation="vertical" >
<WebView
android:background="@android:color/transparent"
android:id="@+id/gifPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
如何在屏幕的中心位置呢?
这已经是在父心:)看到XML :) –