自定义对话框上的WebView在对话框右侧添加了额外的空白-Android

时间:2018-12-26 11:51:58

标签: android android-webview animated-gif

我正在尝试在Android的自定义对话框中添加gif。我已将视图的heightwidth设置为 wrap_content 。尽管如此,它仍然增加了页边距并增加了对话框的宽度。

如果我使用ImageView而不是WebView,则可以正常工作。以下是我的WebView代码,单击按钮即显示对话框:

Button button =  (Button) findViewById(R.id.btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog builder = new Dialog(MainActivity.this);
            builder.setContentView(R.layout.web_view_dialog);
            WebView webView = builder.findViewById(R.id.gif_view);
            webView.setBackgroundColor(Color.TRANSPARENT);
            webView.loadUrl("file:///android_asset/test_gif.gif");
        builder.show();
        }
    });

web_view_dialog.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">

<WebView
    android:id="@+id/gif_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</WebView>

请参考以下gif:

extra_spacing_in_dialogbox_webview

当我们使用ImageView而不是WebView时,它可以工作,但是如果使用WebView,如何从对话框中删除此多余的边距?

1 个答案:

答案 0 :(得分:0)

##Edited  Place this it works well i think
<WebView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gif_view"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginBottom="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />