【问题标题】:how to show a button on a webview without mixing the contents如何在不混合内容的情况下在 webview 上显示按钮
【发布时间】:2014-04-18 05:53:55
【问题描述】:

我正在尝试在 web 视图上创建一个按钮和一个编辑文本,两者都应该位于页面的后面。在下面发布的尝试中,我设法在 web 视图上设置了按钮和编辑文本区域,但问题是,按钮和编辑文本区域都被网页内容遮蔽或覆盖。有什么方法可以避免将内容与这些元素混合。

Java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_web_page00);

    webView00 = (WebView) findViewById(R.id.webView00);
    btnGo = (Button) findViewById(R.id.go_btn);
    etUrl = (EditText) findViewById(R.id.url_edittext);

    webView00.getSettings().setJavaScriptEnabled(true);
    webView00.setWebViewClient(new myWebViewClient());
    webView00.setWebChromeClient(new myWebChromeClient());

    webView00.loadUrl("http://google.com");

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.webapptest01.WebAppTest01$PlaceholderFragment" >

    <WebView
        android:id="@+id/webView00"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/go_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Go!" />

    <EditText
        android:id="@+id/url_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:minWidth="220dp" />

</RelativeLayout>

【问题讨论】:

  • 使用FrameLayout 并制作一个LinearLayout 并将您的按钮添加到此LinearLayout 并将重力设置为此Linearlayout 的底部
  • 我应该使用 FrameLayout 而不是我正在使用的 relativeLayout 吗?

标签: android webview


【解决方案1】:

这是一种将其保留为单个 RelativeLayout(无嵌套布局)并利用 android:layout_above 属性的方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.webapptest01.WebAppTest01$PlaceholderFragment" >

    <Button
        android:id="@+id/go_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Go!" />

    <EditText
        android:id="@+id/url_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:minWidth="220dp" />

    <WebView
        android:id="@+id/webView00"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/url_edittext" />

</RelativeLayout>

【讨论】:

  • 感谢它现在起作用了,但我有一些问题,如果可以的话,请回答它们:1)我怎样才能达到相同的结果,但使用你最初建议的 FrameLayout 和 LinearLayout? 2)为什么我的问题被低估(否决)?
  • 我不建议使用 FrameLayout 和 LinearLayout - 那是其他人。我(当然:))认为我的解决方案更好——它只使用 1 个父布局。为什么你被否决?我不知道,我没有投票给你。这可能是因为您的代码格式不完美(尽管我看到过更糟糕的问题),或者人们认为这是一个简单的问题,而您没有足够“尝试”(我也看到过更糟糕的问题)。 StackOverflow 的人是善变的,不要太在意。
【解决方案2】:

你必须使用框架布局而不是相对布局。

这是你的xml代码,你可以添加这个

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/webView00"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/url_edittext" />

    <Button
        android:id="@+id/go_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:text="Go!" />

    <EditText
        android:id="@+id/url_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:minWidth="220dp" />

</FrameLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2017-12-16
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多