【问题标题】:How to position a Button on top of a WebView in Android如何在 Android 中将 Button 放置在 WebView 的顶部
【发布时间】:2021-03-29 12:53:24
【问题描述】:

我是 Android 开发的新手,我正在研究现有的代码库。

我有一个android.webkit.WebView 实例,我需要在其右下角添加一个按钮。而且我还需要按钮保持固定。我不希望它随着 webview 的内容滚动。我希望按钮始终位于 webview 本身的右下角,而不是其内容。

为此,我扩展了 WebViewClient 类并覆盖了onPageStarted 方法,这就是我正在考虑添加代码以添加按钮的地方。

我的代码如下所示:

public class MyWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        
        //TODO: create a Button instance here and place it on top of the webview
    }
}

为了创建和添加按钮,我尝试了以下不同的选项,但它们都没有像我想要的那样工作。它们都以 webview 最左上角的按钮结束(在 webview 坐标系中的坐标 (0,0) 处),并且当 webview 滚动时,按钮会随着内容滚动。

@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
    super.onPageStarted(webview, url, favicon);

    //TODO: create a Button instance here and place it on top of the webview
    Button button = new Button(webview.getContext());
    button.setText("35");

    RelativeLayout.LayoutParams layoutParams = new 
    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.width = 44;
    layoutParams.height = 44;
    layoutParams.rightMargin = 20;
    layoutParams.bottomMargin = 20;
    button.setLayoutParams(layoutParams);

    webview.addView(button);
}

我也尝试过使用 LinearLayout,但这导致了类似的行为,即按钮位于 Web 视图的顶部,并且按钮随着 Web 视图的内容滚动。

我一直在努力寻找答案,似乎 FrameLayout 也可能是一种方法,但假设这是正确的,我不确定我是否需要这样做 webview.setLayoutParams(new FrameLayout(...)) 或者是否需要这样做而是应用于按钮,还是需要同时设置?

回顾一下,我正在尝试在 Web 视图顶部添加一个按钮,以便:

  • 它位于 Web 视图的右下角。
  • 它不会随着 Web 视图的内容滚动。无论网页视图是否滚动,它都应该固定在同一个右下角位置。

我的问题是:最好的方法是什么(如果可能,以编程方式,而不是使用 XML)。我应该使用 FrameLayout 还是其他东西?

【问题讨论】:

    标签: android android-layout webview android-framelayout


    【解决方案1】:

    我建议您改用 XML,这样您就可以更好地控制各个视图及其在布局中的位置。

    编辑:

    假设您使用 ActivityFragment 作为这些视图的容器,只需将这些视图添加到您的 XML 中即可开始使用它们。

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    
        <WebView
            android:id="webView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    一旦它们位于适当的 XML 文件中,您就可以通过 Activity/Fragment 直接访问它们。

    【讨论】:

    • 感谢您的回答。我只是不确定如何实际调用 XML 文件或如何使用它们。我需要对此进行更多研究。
    • 假设您在 Activity 或 Fragment 中需要这两个 View(Button 和 ViewWeb),您可以简单地将它们添加到与给定 Activity/Fragment 相关的 XML 布局文件中,然后开始使用它们。让我更新我的答案@AlexShipman
    • 很抱歉花了这么长时间才回复,但您的建议效果很好。我最终按照您的建议使用了 XML 布局,它确实简化了事情。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多