【问题标题】:WebView on custom dialog box adds extra margin in right of dialog box - Android自定义对话框上的 WebView 在对话框右侧添加了额外的边距 - Android
【发布时间】:2018-12-26 11:51:58
【问题描述】:

我正在尝试在 android 的自定义对话框中添加 gif。我已将视图的height 和width 设置为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:

当我们使用 ImageView 而不是 WebView 时它可以工作,但是在 WebView 的情况下如何从对话框中删除这个额外的边距?

【问题讨论】:

    标签: android android-webview animated-gif


    【解决方案1】:
    ##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" /> 
    

    【讨论】:

    • 将 parent 布局更改为 wrap_content 并在其中包含 webView 会发出警告为 it may lead to subtle bugs and recommend to use match_parent。尽管如此,我还是通过将其更改为 wrap_content 进行了检查,但它不起作用。
    • 请粘贴上面的代码工作。和依赖..依赖{实现'com.android.support.constraint:constraint-layout:1.1.2'}存储库{google()}
    • 我试过上面的代码。它不工作。得到相同的结果。
    • 你得到相同的约束..请粘贴相同的代码而不做任何更改..它可以工作
    • 当然@satyan_android,请看一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多