2014-07-09 44 views
1

我終於想出了一個(主要是複製粘貼代碼)在應用程序中播放動畫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> 

如何在屏幕的中心位置呢?

回答

0

只需添加在XML文件中的WebView標籤這樣的: 機器人:layout_gravity =「中心」

+0

這已經是在父心:)看到XML :) –

0

特殊照顧的RelativeLayout必須有「FILL_PARENT」的這個寬度工作。

你的編輯應該給你一個警告,至少Android Studio是這樣的。

試試這個:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
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" />