Android如何在同一活动上添加带有按钮的webview

时间:2018-01-10 23:13:21

标签: android button webview

我正在构建一个Android应用程序,我是新手。 我的主要活动上有3个按钮,每个按钮加载不同的网址。 我想在按钮上添加一个WebView URL。按钮已经在底部了。

检查我的主要活动代码 -

public class MainActivity extends Activity {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the button in activity_main.xml
    button = (Button) findViewById(R.id.AlbumButton);

    // album button
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start AlbumActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    AlbumActivity.class);
            startActivity(myIntent);
        }
    });

    // about button
    button = (Button) findViewById(R.id.AboutButton);
    button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            startActivity(new Intent(MainActivity.this, AboutActivity.class));
        }
    });

    // contact button 
    button = (Button) findViewById(R.id.ContactButton);
    button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            startActivity(new Intent(MainActivity.this, ContactActivity.class));
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.activity_main, menu);
    return true;
}

}

我不知道在哪里添加webview。 这是我的activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_dark"
tools:context="com.mayanktaker.moderncolourlab.MainActivity">

<WebView
    android:id="@+id/home_view_browser"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/home_bar"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/background_dark" />

<RelativeLayout
    android:id="@+id/home_bar"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="#000000"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/AlbumButton"
        android:layout_width="85dp"
        android:layout_height="52dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/background_light"
        android:text="@string/AlbumButton"
        android:textColor="@android:color/background_dark"
        android:textSize="14sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="425dp" />


    <Button
        android:id="@+id/AboutButton"
        android:layout_width="85dp"
        android:layout_height="52dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="10dp"
        android:background="@color/colorPrimary"
        android:text="@string/AboutButton"
        android:textColor="@android:color/background_light"
        android:textSize="14sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="425dp" />

    <Button
        android:id="@+id/ContactButton"
        android:layout_width="85dp"
        android:layout_height="52dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="10dp"
        android:background="@color/colorPrimary"
        android:text="@string/ContactButton"
        android:textColor="@android:color/background_light"
        android:textSize="14sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="425dp" />

</RelativeLayout>

我是新手,仍在学习基础知识。 请帮我添加关于此活动的webview。因此,当应用程序打开时,我可以看到webview加载和底部的3个按钮。 (目前获取webview空白区域和底部的按钮) 感谢

3 个答案:

答案 0 :(得分:0)

您可以按以下方式加载网址:

String linkToUrl = "https://www.google.com";
WebView webView = (WebView) findViewById(R.id.home_view_browser);
webView.loadUrl(linkToUrl);

有帮助吗?

答案 1 :(得分:0)

在xml文件中添加以下内容:

   <FrameLayout
            android:id="@+id/webViewContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"/>

....在您的活动中添加以下方法

 public void addWebView(View container){

        WebView webView=new WebView();

        WebView.setWebContentsDebuggingEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollContainer(true);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setGeolocationEnabled(true);
        container.addView(webView);


}

最后在按钮onClick方法调用:

button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
              FrameLayout layout=findViewById(R.id.webViewContainer);

             layout.removeAllViews();
             addWebView(layout);
             webView.loadUrl(YOUR_URL);
        }
    });

答案 2 :(得分:0)

试试这个,

public class MainActivity extends Activity {
    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

         if (Constants.isNetworkAvailable()) {
            WebView htmlWebView = (WebView)findViewById(R.id.home_view_browser);
            htmlWebView.setWebViewClient(new CustomWebViewClient());
            WebSettings webSetting = htmlWebView.getSettings();
            webSetting.setJavaScriptEnabled(true);
            webSetting.setDisplayZoomControls(true);
            htmlWebView.loadUrl("https://www.google.com");
        } else {
              Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_LONG).show();
        }

        // Locate the button in activity_main.xml
        button = (Button) findViewById(R.id.AlbumButton);

        // album button
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                // Start AlbumActivity.class
                Intent myIntent = new Intent(MainActivity.this,
                        AlbumActivity.class);
                startActivity(myIntent);
            }
        });

        // about button
        button = (Button) findViewById(R.id.AboutButton);
        button.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(MainActivity.this, AboutActivity.class));
            }
        });

        // contact button 
        button = (Button) findViewById(R.id.ContactButton);
        button.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(MainActivity.this, ContactActivity.class));
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.layout.activity_main, menu);
        return true;
    }

    private class CustomWebViewClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    /*isNetworkAvailable function*/
    public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        /* if no network is available networkInfo will be null, otherwise check if we are connected*/
        return networkInfo != null && networkInfo.isConnected();
    }
}