如何将重力设置为webview中心以播放gif?

时间:2016-08-02 19:44:52

标签: android android-webview android-alertdialog android-assets

我想在alertdialog中播放gif。

我写了这段代码:

AlertDialog.Builder builder = new AlertDialog.Builder(myThis);
             builder.setCancelable(false);
             WebView view = new WebView(myThis); 
             view.loadUrl("file:///android_asset/gif_pokemon.gif");
             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
             params.gravity=Gravity.CENTER;
             view.setLayoutParams(params);
             builder.setView(view);
             dlg_p=builder.create();
             WindowManager.LayoutParams wlmp = dlg_p.getWindow().getAttributes();       
             wlmp.gravity = Gravity.CENTER ; 
             dlg_p.show();

但问题是gif不在中心位置。它的引力留下了。

如何将其设置为中心?

1 个答案:

答案 0 :(得分:0)

使用LinearLayout

包装WebView
  • LinearLayout的LayoutParams设置为match_parent为width,wrap_content设置为height,如果需要完全居中,则也使用match_parent作为高度。

  • 将LinearLayout引力设置为居中,您也可以使用centerVertical或centerHorizo​​ntal。

希望有所帮助:)

AlertDialog.Builder builder = new AlertDialog.Builder(myThis);         builder.setCancelable(假);

    LinearLayout linearLayout = new LinearLayout(myThis);
    linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayout.setGravity(Gravity.CENTER);

    WebView view = new WebView(myThis);
    view.loadUrl("file:///android_asset/gif_pokemon.gif");
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.gravity=Gravity.CENTER;
    view.setLayoutParams(params);

    linearLayout.addView(view);
    builder.setView(linearLayout);
    dlg_p=builder.create();
    dlg_p.show();