按钮不会显示在webview旁边

时间:2015-01-14 16:03:22

标签: android android-layout button webview tabs


我的应用程序包含一个tab-widged活动。在一个选项卡中,我显示了一个webview,我想在其中显示几个按钮。
我发布的代码现在有效!
感谢您的帮助

package com.whateveryoulike.whateveryoulike;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Activity_tab_reader extends Activity {

private WebView webView;

// entnimmt die url aus dem public static string und legt sie in der localen string    mynewurl ab.
String MyNewUrl =  Activity_Haupt.MyDocumentURL;
//String data = "<html><head></head><body>this should be my long long text...</body></html>";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_reader);

    MyNewUrl =  Activity_Haupt.MyDocumentURL;
    webView = (WebView) findViewById(R.id.webview);
    webView.loadUrl(MyNewUrl);

    //Settings webview
    webView.getSettings().setJavaScriptEnabled(true); // enable javascript
    webView.getSettings().setSaveFormData(true);

    final Activity activity = this;

    webView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });     
}

    @Override
    protected void onResume() {
        super.onResume();
        if(!MyNewUrl.equals(Activity_Haupt.MyDocumentURL)){

            MyNewUrl =  Activity_Haupt.MyDocumentURL;   
            webView.loadUrl(MyNewUrl);

        }
    }
}

这是tabxmlfile:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fh_blau"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <Button
            android:id="@+id/btnRefresh"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/btnCloseSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/linear" />

</RelativeLayout>

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

根据您编辑的问题,下面的代码应该可以使用,当您使用RelativeLayout时,您必须让小部件知道它们需要的位置,使用layout_above / layout_below和RelativeLayout的属性 编辑:我添加了setContentView(R.id.tab)更改“标签”与您的xml文件名。 并在复制粘贴之前尝试学习代码

您的活动,

 public class Activity_tab_reader extends Activity {

    private WebView webView;

    // entnimmt die url aus dem public static string und legt sie in der localen string mynewurl ab.
    String MyNewUrl =  Activity_Haupt.MyDocumentURL;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);
        webView  = (WebView) findViewById(R.id.webview);
//    do the same as above here to initialize buttons and handle their clicks.
    MyNewUrl =  Activity_Haupt.MyDocumentURL;   
        webView.loadUrl(MyNewUrl);
        //webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "utf-8", null);
        webView.getSettings().setJavaScriptEnabled(true); // enable javascript
        webView.getSettings().setSaveFormData(true);
        //webView.getSettings().setBuiltInZoomControls(true);
        //webView.getSettings().setSupportMultipleWindows(true);
        final Activity activity = this;

        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });
    //  webView .loadUrl("http://www.google.com");

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!MyNewUrl.equals(Activity_Haupt.MyDocumentURL)){

            MyNewUrl =  Activity_Haupt.MyDocumentURL;   
            webView.loadUrl(MyNewUrl);
        }
    }

和标签xmlfile,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fh_blau"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <Button
            android:id="@+id/btnRefresh"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/btnCloseSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/linear" />

</RelativeLayout>