首页 > 解决方案 > 如何在Bottonsheetdialog android studio中显示网站

问题描述

如何在 BottonSheetDialog 中显示带有 webview 的网页!像这样

单击此处查看示例图像

标签: javaandroidandroid-fragmentsandroid-bottomsheetdialog

解决方案


要做到这一点,您必须创建一个扩展类BottomSheetDialogFragment,您必须创建一个包含 a 的布局webview,然后调用oncreateview()您膨胀布局的方法并webview使用布局中的引用设置 ,然后在您的活动中显示bottomsheetdialog

*首先创建一个布局:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"/>

</androidx.constraintlayout.widget.ConstraintLayout>
  • 其次创建一个扩展 BottomSheetDialogFragment 的类
//Extend Your class with BottomSheetDialogFragment
public class WebViewBottomSheet extends BottomSheetDialogFragment{

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        
        //Inflate Your layout which contains the webView
        View view = inflater.inflate(R.layout.weblayout,container,false);

        WebView webView = view.findViewById(R.id.webview);
        webView.loadUrl("Put your URL here");
        return view;
    }
}

  • 最后,只需在您的活动中显示您的 BottomSheet
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        //Here you show your BottomSheetDialog
        WebViewBottomSheet webViewBottomSheet = new WebViewBottomSheet();
        webViewBottomSheet.show(getSupportFragmentManager(),"Show Bottom Sheet");
    }
}

PS:不要忘记将设计库添加到您的项目中


推荐阅读