效果
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tl_tabs"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabSelectedTextColor="#FF0000"
app:tabTextColor="#000000" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
- fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/fragm_web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
- MainActivity.java
package com.example.tablayoutdemo;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout tlTabs;
private ViewPager vpContent;
private static final String TAG = "MainActivity";
List<Fragment> fragments = new ArrayList<>();
List<String> titles = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
fragments.add(new MyFragment("https://www.baidu.com"));
fragments.add(new MyFragment("https://www.zhihu.com"));
fragments.add(new MyFragment("https://www.sogou.com"));
fragments.add(new MyFragment("https://cn.bing.com"));
titles.add("百度");
titles.add("知乎");
titles.add("搜狗");
titles.add("Bing");
vpContent.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
});
tlTabs.setupWithViewPager(vpContent);
}
private void initViews() {
tlTabs = findViewById(R.id.tl_tabs);
vpContent = findViewById(R.id.vp_content);
}
public static class MyFragment extends Fragment {
private String url = "";
public MyFragment(String url) {
this.url = url;
}
private WebView webView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
webView = view.findViewById(R.id.fragm_web);
WebSettings webSettings = webView.getSettings();
// 让WebView能够执行javaScript
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
// 让JavaScript可以自动打开windows
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAppCacheEnabled(true);
// 将图片调整到合适的大小
webSettings.setUseWideViewPort(true);
// 缩放至屏幕的大小
webSettings.setLoadWithOverviewMode(true);
// 支持缩放(适配到当前屏幕)
webSettings.setSupportZoom(true);
// 设置编码
webSettings.setDefaultTextEncodingName("utf-8");
webView.loadUrl(url);
//实现:WebView里的链接,都在自身打开,不调用系统浏览器
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, ">>>" + url);
try {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
return true;
} catch (Exception e) {
return false;
}
}
});
//实现:按手机回退键,如果浏览器有上一个网页,则返回上一个网页
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
}
return false;
}
});
return view;
}
}
}