【问题标题】:webview showing webpages down the main layoutwebview 在主布局下方显示网页
【发布时间】:2012-06-22 19:53:53
【问题描述】:

在这张图片中,我单击了 google 按钮,它在 web 视图中显示了 google 网站,但它也显示了其上方应用程序的主要布局(两个按钮)。我想在单击 google 按钮时实现只有网站应该出现在 webview 中,而不是上面的按钮。这是我的 java 和 xml 代码。提前致谢。

public class TestinglinkActivity extends Activity {
     final Activity activity = this;
     WebView webview;

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


        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new MyWebViewClient());



    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {

            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });


    Button btn_google = (Button) findViewById(R.id.btn_click_login);
    btn_google.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             webview.loadUrl("http://www.google.com.pk");
             webview.goBack();
             webview.goForward();


          }
         }
     );


    Button btn_gmail = (Button) findViewById(R.id.btn_gmail);
    btn_gmail.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             webview.loadUrl("http://www.gmail.com");
             webview.goBack();
             webview.goForward();


          }
         }
     );

           }   
};
}

xml

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" >  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button 
            android:id="@+id/btn_click_login"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2"
            android:text="Google"/>

    <Button 
            android:id="@+id/btn_gmail"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2"
            android:text="Gmail"/>

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

</LinearLayout>



</ScrollView>

【问题讨论】:

    标签: android android-layout android-webview


    【解决方案1】:

    使用 setVisibility(View.GONE) 在点击时隐藏按钮:

    Button btn_gmail = (Button) findViewById(R.id.btn_gmail);
    Button btn_google = (Button) findViewById(R.id.btn_click_login);
        btn_google.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
    
                 webview.loadUrl("http://www.google.com.pk");
                 btn_google.setVisibility(View.GONE);
                 btn_gmail.setVisibility(View.GONE);
                 webview.goBack();
                 webview.goForward();
    
    
              }
             }
         );
    

    【讨论】:

    • 我不想在单击时隐藏按钮,当我单击其中一个按钮时,只有一个按钮不可见,如果我在按钮下方有一些文本视图怎么办。我不想在点击后隐藏按钮。我想通过 webview 覆盖主布局 .like 新活动。
    • ps。如何在按钮单击时消失 LinearLayout
    【解决方案2】:

    使用下面的代码

    btn_google = (Button) findViewById(R.id.btn_click_login);
    btn_google.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            webview.loadUrl("http://www.google.com.pk");
            webview.goBack();
            webview.goForward();
            TestinglinkActivity.this.btn_google.setVisibility(View.GONE);
            TestinglinkActivity.this.btn_gmail.setVisibility(View.GONE);
        }
    });
    
    btn_gmail = (Button) findViewById(R.id.btn_gmail);
    btn_gmail.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            webview.loadUrl("http://www.gmail.com");
            webview.goBack();
            webview.goForward();
            TestinglinkActivity.this.btn_google.setVisibility(View.GONE);
            TestinglinkActivity.this.btn_gmail.setVisibility(View.GONE);
        }
    });
    

    而不是

    Button btn_google = (Button) findViewById(R.id.btn_click_login);
    btn_google.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
                 webview.loadUrl("http://www.google.com.pk");
                 webview.goBack();
                 webview.goForward();
          }
    });
    
    
    Button btn_gmail = (Button) findViewById(R.id.btn_gmail);
    btn_gmail.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
                 webview.loadUrl("http://www.gmail.com");
                 webview.goBack();
                 webview.goForward();
          }
    });
    

    并在全球范围内声明按钮 btn_google 和 btn_gmail。

    【讨论】:

    • 我不想在单击时隐藏按钮,当我单击其中一个按钮时,只有一个按钮不可见,如果我在按钮下方有一些文本视图怎么办。我不想在点击后隐藏按钮。我想通过 webview 覆盖主布局 .like 新活动。
    • 当我从我的主 xml 中删除滚动视图时,webview 会显示在整个屏幕上。但我也想使用滚动视图。希望这能帮助您解决问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多