两个 Web 视图在 Android 中垂直对齐



我是Android编程的新手,我想显示两个Web视图,第二个在第一个垂直下方对齐。我希望它们同时显示。我已经尝试了有关该主题的不同帖子,但示例代码似乎显示在不同的活动或窗口中。

这是我开发的内容,但我需要您关于如何解决这个问题的想法

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView2"
/>
<WebView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView2"
/>
</LinearLayout>

这是连接包含两个 Web 视图的activity_main.xml的示例 java 文件主要活动.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity{
private WebView webView1, webView2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView1 = (WebView) findViewById(R.id.webView);
webView2 = (WebView) findViewById(R.id.webView2);
webView1.setWebViewClient(new MyWebViewClient1());
webView1.getSettings().setJavaScriptEnabled(true);
webView1.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView1.getSettings().setDomStorageEnabled(true);
webView1.getSettings().setSupportMultipleWindows(true);
webView1.getSettings().setAppCacheEnabled(true);
webView1.getSettings().setAllowFileAccess(true);
webView1.getSettings().setLoadsImagesAutomatically(true);
webView2.setWebViewClient(new MyWebViewClient2());
webView2.getSettings().setJavaScriptEnabled(true);
webView2.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView2.getSettings().setDomStorageEnabled(true);
webView2.getSettings().setSupportMultipleWindows(true);
webView2.getSettings().setAppCacheEnabled(true);
webView2.getSettings().setAllowFileAccess(true);
webView2.getSettings().setLoadsImagesAutomatically(true);
webView1.loadUrl("http://google.com");
webView2.loadUrl("http://stackoverflow.com");

}

}
@Override
public void onRefresh() {
initView();
}
public class JavaScriptReceiver
{
Context mContext;
/** Instantiate the receiver and set the context */
JavaScriptReceiver(Context c) {
mContext = c;
}
@JavascriptInterface
public void showProfileDetails(){
Intent intent = new Intent(mContext, DetailActivity.class);
mContext.startActivity(intent);
}
}

private class MyWebViewClient1 extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(url)) {  
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
private class MyWebViewClient2 extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(url)) {  
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// adds item to action bar
getMenuInflater().inflate(R.menu.menu_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.add_post) {
Intent intent = new Intent(getApplicationContext(), PostActivity.class);
startActivity(intent);
return true;
}
if (item.getItemId() == R.id.upload) {
Intent intent = new Intent(getApplicationContext(), CustomChooserActivity.class);
startActivity(intent);
return true;
}
if (item.getItemId() == R.id.action_settings) {
Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}

在 LinearLayout 中,将方向设置为垂直而不是水平。还可以使用权重属性来正确对齐布局。

最新更新