前言: 之前设计了集中布局 因为我想要控件,有要布局合理, 一般用match_parent 这样能适应个各种手机型号,因为以为写的只使用虚拟机,到真机上不要用 多少dp啊什么的,有一种特别别扭的感觉,可能我六大布局,还不会灵活的运用吧 ,已经改善多次,我觉这个布局还是自己觉得不错的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="com.example.l.instructordome.WebviewActivity">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_white"
android:padding="0dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageButton
android:id="@+id/back_btn"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentLeft="true"
android:background="@drawable/tab_menu_bg"
android:onClick="editInfoBackTo"
android:src="?attr/homeAsUpIndicator"/>
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/bg_black"
android:textSize="19sp"/>
<Button
android:id="@+id/btn_refresh"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:layout_toLeftOf="@+id/btn_top"
android:layout_toStartOf="@+id/btn_top"
android:background="@color/blue"
android:text="刷新"
android:textColor="@color/bg_white"/>
<Button
android:id="@+id/btn_top"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:background="@color/blue"
android:text="Top"
android:textColor="@color/bg_white"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_below="@+id/relativeLayout">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
</WebView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ImageButton
android:id="@+id/zuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/bg_white"
app:srcCompat="@android:drawable/ic_media_rew"/>
<ImageButton
android:id="@+id/you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/bg_white"
app:srcCompat="@android:drawable/ic_media_ff"/>
</LinearLayout>
</RelativeLayout>
public class WebviewActivity extends AppCompatActivity implements View.OnClickListener{
TextView title;
private WebView webView;
private ImageButton back_btn;
private Button btn_top;
private Button btn_refresh;
private ImageButton zou;
private ImageButton you;
String name ="数字渤大";
String url="http://cas.bhu.edu.cn/cas/login?service=http://i.bhu.edu.cn:80/dcp/index.jsp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
init();
}
private void init() {
webView = (WebView) findViewById(R.id.webView);
// 覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在WebVIew中打开
WebSettings webSettings = webView.getSettings();
//声明WebSettings子类
// 如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript
webSettings.setJavaScriptEnabled(true);
//设置自适应屏幕,两者合用
webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
// 缩放操作
webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。
webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放
webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
// 其他细节操作
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
webSettings.setAllowFileAccess(true); //设置可以访问文件
webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
title=(TextView)findViewById(R.id.title_text);
title.setText(name);
back_btn=(ImageButton)findViewById(R.id.back_btn);
back_btn.setOnClickListener(this);
zou=(ImageButton)findViewById(R.id.zuo);
you=(ImageButton)findViewById(R.id.you);
zou.setOnClickListener(this);
you.setOnClickListener(this);
btn_top = (Button) findViewById(R.id.btn_top);
btn_refresh = (Button) findViewById(R.id.btn_refresh);
btn_refresh.setOnClickListener(this);
btn_top.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_btn:
finish();
break;
case R.id.zuo:
if (webView.canGoBack()) {
webView.goBack();
} else {
Toast.makeText(WebviewActivity.this, "已经是第一页!", Toast.LENGTH_LONG).show();
return;
}
break;
case R.id.you:
if (webView.canGoForward()) {
webView.goForward();
} else {
Toast.makeText(WebviewActivity.this, "已经是最后一页!", Toast.LENGTH_LONG).show();
return;
}
break;
case R.id.btn_refresh:
webView.reload(); //刷新当前页面
break;
case R.id.btn_top:
webView.setScrollY(0); //滚动到顶部
break;
}
}
}
注:如果跳转的网页有垃圾广告的话,还有写个过滤器。
这里我一条url 跳转,我比较想只用一个activity和layout 跳转不同的url;
这里我想了很多最后,也试了很多方式,比如 创建一个url实例啊,传递参数也有多种方式啊等等,最后就这种方式实现了,不知道,大家有没有更简单的方式,或者算法什么的。
gridView01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0&&(int)l==0) {
Intent intent=new Intent(getActivity(),WebviewActivity.class);
Bundle B=new Bundle();
B.putString("name","数字渤大");
B.putString("url","http://cas.bhu.edu.cn/cas/login?service=http://i.bhu.edu.cn:80/dcp/index.jsp");
intent.putExtra("data",B);
startActivity(intent);
}
if(i==1&&(int)l==0) {
Intent intent=new Intent(getActivity(),WebviewActivity.class);
Bundle B=new Bundle();
B.putString("name","执行测评");
B.putString("url","http://210.47.176.33:8080/Default.aspx?url=s_App8.aspx");
intent.putExtra("data",B);
startActivity(intent);
}
private void selectUrl() {
Intent i=getIntent();
Bundle b=new Bundle();
b=i.getBundleExtra("data");
url=b.getString("url");
name=b.getString("name");
Toast.makeText(WebviewActivity.this, name, Toast.LENGTH_SHORT).show();
}
重复的部分我就不放了,这里分类+轮播广告 一共跳转了18个网址。不用写那么多webview ,我以前还真傻傻的写过,我就觉得能简化,哈哈。如果有更好的方法,求支招,虚心请教。有些东西我可能微调,不影响思想。
这里就属于 Activity 传递数据的问题,有多种方式,我用了这一种。