顶部新闻轮播图事件处理
重写dispatchTouchEvent,并且要在按下的时候
getParent().requestDisallowInterceptTouchEvent(true);
判断滑动方向,在X轴和Y轴滑动的绝对值谁大就是在什么方向滑动
一,竖直方向滑动
getParent().requestDisallowInterceptTouchEvent(false);
二,水平方向滑动
2.1,当滑动到ViewPager的第0个页面,并且是从左到右滑动
getParent().requestDisallowInterceptTouchEvent(false);
2.2,当滑动到ViewPager的最后一个页面,并且是从右到左滑动
getParent().requestDisallowInterceptTouchEvent(false);
2.3,其他
getParent().requestDisallowInterceptTouchEvent(true);
自定义HorizontalScrollViewPager
/**
* @author: Hashub
* @WeChat: NGSHMVP
* @Date: 2018/9/9 18:46
* @function:水平方向滑动的ViewPager
*/
public class HorizontalScrollViewPager extends ViewPager
{
/**
* 起始坐标
*/
private float startX;
private float startY;
public HorizontalScrollViewPager(Context context)
{
super(context);
}
public HorizontalScrollViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
//请求父层视图不拦截,当前控件的事件
getParent().requestDisallowInterceptTouchEvent(true);//都把事件传给当前控件(HorizontalScrollViewPager)
//1.记录起始坐标
startX = ev.getX();
startY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
//2.来到新的坐标
float endX = ev.getX();
float endY = ev.getY();
//3.计算偏移量
float distanceX = endX - startX;
float distanceY = endY - startY;
//4.判断滑动方向
if (Math.abs(distanceX) > Math.abs(distanceY))
{
//水平方向滑动
//2.1,当滑动到ViewPager的第0个页面,并且是从左到右滑动
//getParent().requestDisallowInterceptTouchEvent(false);
if (getCurrentItem() == 0 && distanceX > 0)
{
getParent().requestDisallowInterceptTouchEvent(false);
}
//2.2,当滑动到ViewPager的最后一个页面,并且是从右到左滑动
// getParent().requestDisallowInterceptTouchEvent(false);
else if ((getCurrentItem() == (getAdapter().getCount() - 1)) && distanceX < 0)
{
getParent().requestDisallowInterceptTouchEvent(false);
}
//2.3,其他,中间部分
//getParent().requestDisallowInterceptTouchEvent(true);
else
{
getParent().requestDisallowInterceptTouchEvent(true);
}
}
else
{
//竖直方向滑动
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
}
自定义下拉刷新(touch事件处理
1.自定义Listview实现下拉刷新,RefreshListview,必须重新带有两个参数的构造方法
2.在构造方法里面,把下拉刷新控件添加
自定义ProgressBar
3.下拉刷新控件隐藏和显示的原理
View.setPadding(0,-控件高,0,0);//完全隐藏
View.setPadding(0,0,0,0);//完全显示
View.setPadding(0,控件高,0,0);//2倍高显示
4.拖动实现隐藏和显示下拉刷新控件
a,重写onTouchEvent()
在down startY;
b,计算滑动的距离
float distanceY = endY - startY;
int paddingTop = -控件高 + distanceY;
View.setPadding(0,paddingTop,0,0);//动态的显示下拉刷新控件
c,设置效果
刷新的效果的实现
1.定义刷新的三个状态
/**
* 下拉刷新控件的高
*/
private int pullDownRefreshHeight;
/**
下拉刷新
*/
public static final int PULL_DOWN_REFRESH = 0;
/**
手松刷新
*/
public static final int RELEASE_REFRESH = 1;
/**
正在刷新
*/
public static final int REFRESHING = 2;
/**
* 当前状态
*/
private int currentStatus = PULL_DOWN_REFRESH;
自定义下拉刷新RefreshListview
/**
* @author: Hashub
* @WeChat: NGSHMVP
* @Date: 2018/9/6 19:37
* @function:自定义下拉刷新
*/
public class RefreshListview extends ListView
{
/**
* 下拉刷新和顶部轮播图
*/
private LinearLayout headerView;
/**
* 下拉刷新控件
*/
private View ll_pull_down_refresh;
private ImageView iv_arrow;
private ProgressBar pb_status;
private TextView tv_status;
private TextView tv_time;
/**
* 下拉刷新控件的高
*/
private int pullDownRefreshHeight;
/**
* 下拉刷新
*/
public static final int PULL_DOWN_REFRESH = 0;
/**
* 手松刷新
*/
public static final int RELEASE_REFRESH = 1;
/**
* 正在刷新
*/
public static final int REFRESHING = 2;
/**
* 当前状态
*/
private int currentStatus = PULL_DOWN_REFRESH;
private Animation upAnimation;
private Animation downAnimation;
/**
* 加载更多的控件
*/
private View footerView;
/**
* 加载更多控件高
*/
private int footerViewHeight;
/**
* 是否已经加载更多
*/
private boolean isLoadMore = false;
/**
* 顶部轮播图部分
*/
private View topNewsView;
/**
* ListView在Y轴上的坐标
*/
private int listViewOnScreenY = -1;
public RefreshListview(Context context)
{
this(context, null);
}
public RefreshListview(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public RefreshListview(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initHeaderView(context);
initAnimation();
initFooterView(context);
}
private void initFooterView(Context context)
{
footerView = View.inflate(context, R.layout.refresh_footer, null);
footerView.measure(0, 0);
footerViewHeight = footerView.getMeasuredHeight();
footerView.setPadding(0, -footerViewHeight, 0, 0);
//ListView添加footer
addFooterView(footerView);
//监听ListView的滚动
setOnScrollListener(new MyOnScrollListener());
}
/**
* 添加顶部轮播图
*
* @param topNewsView
*/
public void addTopNewsView(View topNewsView)
{
if (topNewsView != null)
{
this.topNewsView = topNewsView;
headerView.addView(topNewsView);
}
}
class MyOnScrollListener implements OnScrollListener
{
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
//当静止或者惯性滚动的时候
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE || scrollState == OnScrollListener.SCROLL_STATE_FLING)
{
//并且是最后一条可见
if (getLastVisiblePosition() >= getCount() - 1)
{
//1.显示加载更多布局
footerView.setPadding(8, 8, 8, 8);
//2.状态改变
isLoadMore = true;
//3.回调接口
if (mOnRefreshListener != null)
{
mOnRefreshListener.onLoadMore();
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
}
}
private void initAnimation()
{
upAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
upAnimation.setDuration(500);
upAnimation.setFillAfter(true);
downAnimation = new RotateAnimation(-180, -360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
downAnimation.setDuration(500);
downAnimation.setFillAfter(true);
}
private void initHeaderView(Context context)
{
headerView = (LinearLayout) View.inflate(context, R.layout.refresh_header, null);
//下拉刷新控件
ll_pull_down_refresh = headerView.findViewById(R.id.ll_pull_down_refresh);
iv_arrow = (ImageView) headerView.findViewById(R.id.iv_arrow);
pb_status = (ProgressBar) headerView.findViewById(R.id.pb_status);
tv_status = (TextView) headerView.findViewById(R.id.tv_status);
tv_time = (TextView) headerView.findViewById(R.id.tv_time);
//测量
ll_pull_down_refresh.measure(0, 0);
pullDownRefreshHeight = ll_pull_down_refresh.getMeasuredHeight();
//默认隐藏下拉刷新控件
// View.setPadding(0,-控件高,0,0);//完全隐藏
//View.setPadding(0, 0,0,0);//完全显示
ll_pull_down_refresh.setPadding(0, -pullDownRefreshHeight, 0, 0);
//添加ListView的头
addHeaderView(headerView);
}
private float startY = -1;
@Override
public boolean onTouchEvent(MotionEvent ev)
{
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
//1.记录起始坐标
startY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
if (startY == -1)
{
startY = ev.getY();
}
//判断顶部轮播图是否完全显示,只有完全显示才会有下拉刷新
boolean isDisplayTopNews = isDisplayTopNews();
if (!isDisplayTopNews)
{
//加载更多
break;
}
//如果是正在刷新,就不让再刷新了
if (currentStatus == REFRESHING)
{
break;
}
//2.来到新的坐标
float endY = ev.getY();
//3.记录滑动的距离
float distanceY = endY - startY;
if (distanceY > 0)
{//下拉
//int paddingTop = -控件高 + distanceY;
int paddingTop = (int) (-pullDownRefreshHeight + distanceY);
if (paddingTop < 0 && currentStatus != PULL_DOWN_REFRESH)
{
//下拉刷新状态
currentStatus = PULL_DOWN_REFRESH;
//更新状态
refreshViewState();
}
else if (paddingTop > 0 && currentStatus != RELEASE_REFRESH)
{
//手松刷新状态
currentStatus = RELEASE_REFRESH;
//更新状态
refreshViewState();
}
ll_pull_down_refresh.setPadding(0, paddingTop, 0, 0);
//View.setPadding(0,paddingTop,0,0);//动态的显示下拉刷新控件
}
break;
case MotionEvent.ACTION_UP:
startY = -1;
if (currentStatus == PULL_DOWN_REFRESH)
{
// View.setPadding(0,-控件高,0,0);//完全隐藏
ll_pull_down_refresh.setPadding(0, -pullDownRefreshHeight, 0, 0);
}
else if (currentStatus == RELEASE_REFRESH)
{
//设置状态为正在刷新
currentStatus = REFRESHING;
refreshViewState();
// View.setPadding(0,0,0,0);//完全显示
ll_pull_down_refresh.setPadding(0, 0, 0, 0);
//回调接口
if (mOnRefreshListener != null)
{
mOnRefreshListener.onPullDownRefresh();
}
}
break;
}
return super.onTouchEvent(ev);
}
/**
* 判断是否完全显示顶部轮播图
* 当ListView在屏幕上的Y轴坐标小于或者等于顶部轮播图在Y轴的坐标的时候,顶部轮播图完全显示
*
* @return
*/
private boolean isDisplayTopNews()
{
if (topNewsView != null)
{
//1.得到ListView在屏幕上的坐标
int[] location = new int[2];
if (listViewOnScreenY == -1)
{
getLocationOnScreen(location);
listViewOnScreenY = location[1];
}
//2.得到顶部轮播图在屏幕上的坐标
topNewsView.getLocationOnScreen(location);
int topNewsViewOnScreenY = location[1];
// if(listViewOnScreenY <= topNewsViewOnScreenY){
// return true;
// }else{
// return false;
// }
return listViewOnScreenY <= topNewsViewOnScreenY;
}
else
{
return true;
}
}
private void refreshViewState()
{
switch (currentStatus)
{
case PULL_DOWN_REFRESH://下拉刷新状态
iv_arrow.startAnimation(downAnimation);
tv_status.setText("下拉刷新...");
break;
case RELEASE_REFRESH://手松刷新状态
iv_arrow.startAnimation(upAnimation);
tv_status.setText("手松刷新...");
break;
case REFRESHING://正在刷新状态
tv_status.setText("正在刷新...");
pb_status.setVisibility(VISIBLE);
iv_arrow.clearAnimation();
iv_arrow.setVisibility(GONE);
break;
}
}
/**
* 当联网成功和失败的时候回调该方法
* 用户刷新状态的还原
*
* @param sucess
*/
public void onRefreshFinish(boolean sucess)
{
if (isLoadMore)
{
//加载更多
isLoadMore = false;
//隐藏加载更多布局
footerView.setPadding(0, -footerViewHeight, 0, 0);
}
else
{
//下拉刷新
tv_status.setText("下拉刷新...");
currentStatus = PULL_DOWN_REFRESH;
iv_arrow.clearAnimation();
pb_status.setVisibility(GONE);
iv_arrow.setVisibility(VISIBLE);
//隐藏下拉刷新控件
ll_pull_down_refresh.setPadding(0, -pullDownRefreshHeight, 0, 0);
if (sucess)
{
//设置最新更新时间
tv_time.setText("上次更新时间:" + getSystemTime());
}
}
}
/**
* 得到当前Android系统的时间
*
* @return
*/
private String getSystemTime()
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
}
/**
* 监听控件的刷新
*/
public interface OnRefreshListener
{
/**
* 当下拉刷新的时候回调这个方法
*/
public void onPullDownRefresh();
/**
* 当加载更多的时候回调这个方法
*/
public void onLoadMore();
}
private OnRefreshListener mOnRefreshListener;
/**
* 设置监听刷新,由外界设置
*/
public void setOnRefreshListener(OnRefreshListener l)
{
this.mOnRefreshListener = l;
}
}
PullToRefresh下拉刷新的使用
1.下载
https://github.com/chrisbanes/Android-PullToRefresh
2.导入运行PullToRefresh案例
3.参照案例,关联库
4.专题页面也实现了新闻详情页面的效果
5.使用PullToRefresh
加载布局,且PullRefreshListView.getRefreshableView();得到返回值为ListView类型
@Override
public View initView()
{
View view = View.inflate(context, R.layout.topic_detail_pager, null);
mPullRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
listview = mPullRefreshListView.getRefreshableView();
/**
* Add Sound Event Listener
*/
SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(context);
soundListener.addSoundEvent(PullToRefreshBase.State.PULL_TO_REFRESH, R.raw.pull_event);
soundListener.addSoundEvent(PullToRefreshBase.State.RESET, R.raw.reset_sound);
soundListener.addSoundEvent(PullToRefreshBase.State.REFRESHING, R.raw.refreshing_sound);
mPullRefreshListView.setOnPullEventListener(soundListener);
View topNewsView = View.inflate(context, R.layout.topnews, null);
viewpager = (HorizontalScrollViewPager) topNewsView.findViewById(R.id.viewpager);
tv_title = (TextView) topNewsView.findViewById(R.id.tv_title);
ll_point_group = (LinearLayout) topNewsView.findViewById(R.id.ll_point_group);
//把顶部轮播图部分视图,以头的方式添加到ListView中
listview.addHeaderView(topNewsView);
// listview.addTopNewsView(topNewsView);
//设置监听下拉刷新
// listview.setOnRefreshListener(new MyOnRefreshListener());
mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>()
{
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
{
getDataFromNet();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
{
if (TextUtils.isEmpty(moreUrl))
{
//没有更多数据
Toast.makeText(context, "没有更多数据", Toast.LENGTH_SHORT).show();
// listview.onRefreshFinish(false);
mPullRefreshListView.onRefreshComplete();
}
else
{
getMoreDataFromNet();
}
}
});
return view;
}
设置ListView的item的点击监听
class MyOnItemClickListener implements AdapterView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
int realPosition = position - 1;
TabDetailPagerBean.DataEntity.NewsData newsData = news.get(realPosition);
// Toast.makeText(context, "newsData==id=="+newsData.getId()+",newsData_title=="+newsData.getTitle(), Toast.LENGTH_SHORT).show();
LogUtil.e("newsData==id==" + newsData.getId() + ",newsData_title==" + newsData.getTitle() + ",url===" + newsData.getUrl());
//1,取出保存的id集合
String idArray = CacheUtils.getString(context, READ_ARRAY_ID);//"3511,"
//2,判断是否存在,如果不存在,才保存,并且刷新适配器
if (!idArray.contains(newsData.getId() + ""))
{//3512
CacheUtils.putString(context, READ_ARRAY_ID, idArray + newsData.getId() + ",");//"3511,3512,"
//刷新适配器
adapter.notifyDataSetChanged();//getCount-->getView
}
//跳转到新闻浏览页面
Intent intent = new Intent(context, NewsDetailActivity.class);
intent.putExtra("url", Constants.BASE_URL + newsData.getUrl());
context.startActivity(intent);
}
}
设置点击过的listview的item条目文字标题变灰色
//在ListView的适配器TabDetailPagerListAdapter的getView方法中
String idArray = CacheUtils.getString(context, READ_ARRAY_ID);
if (idArray.contains(newsData.getId() + ""))
{
//设置灰色
viewHolder.tv_title.setTextColor(Color.GRAY);
}
else
{
//设置黑色
viewHolder.tv_title.setTextColor(Color.BLACK);
}
TabLayout的使用
TabLayout原生的用法
参照网址:
https://segmentfault.com/a/1190000003500271
1.关联库
compile 'com.android.support:design:23.3.0'
2.布局写上TabLayout
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
3.实例化
@ViewInject(R.id.tablayout)
private TabLayout tabLayout;
4.TabLayout和ViewPager关联
tabLayout.setupWithViewPager(viewPager);
//注意以后监听页面的变化 ,TabPageIndicator监听页面的变化
viewPager.addOnPageChangeListener(new MyOnPageChangeListener());
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
5.适配器中重写getPageTitle方法
class MyNewsMenuDetailPagerAdapter extends PagerAdapter {
@Override
public CharSequence getPageTitle(int position) {
return children.get(position).getTitle();
}
.....
}
6.解决TabLayout和ViewPagerIndicator的兼容问题
<activity
android:name=".activity.MainActivity"
android:theme="@style/Theme.PageIndicatorDefaults" />
6.2 ViewPagerIndicator库修改成如下
<style name="Theme.PageIndicatorDefaults" parent="Theme.AppCompat.Light.NoActionBar">
<item name="vpiIconPageIndicatorStyle">@style/Widget.IconPageIndicator</item>
<item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item>
</style>
6.3添加V7包
compile 'com.android.support:appcompat-v7:23.3.0'
TabLayout设置指针颜色和高度&文字颜色
1.添加样式
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
style="@style/MyCustomTabLayout"
android:layout_height="wrap_content"
android:layout_weight="1" />
-
values目录下styles.xml里面
MyCustomTabLayout样式 <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout"> <item name="tabMaxWidth">72dp</item> <item name="tabMinWidth">72dp</item> <item name="tabIndicatorColor">#ff0000</item> <item name="tabIndicatorHeight">2dp</item> <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item> <item name="tabSelectedTextColor">@android:color/holo_red_light</item> </style> <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab"> <item name="android:textSize">16sp</item> <item name="android:textColor">@android:color/black</item> <item name="textAllCaps">false</item> </style>
TabLayout自定义样式
1.设置的样式
vp_news_menu_detailpager.setAdapter(adapter);
//6.关联ViewPager,TabPageIndicator才可以显示
tabLayout.setupWithViewPager(vp_news_menu_detailpager);
//设置滚动模式
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//设置页面的监听需要用TabPageIndicator
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(getTabView(i));
}
注意了setupWithViewPager必须在ViewPager.setAdapter()之后调用
//自定义Tab
2.在适配器中添加getTabView()方法
public View getTabView(int position){
View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
TextView tv= (TextView) view.findViewById(R.id.textView);
tv.setText(childrenDatas.get(position).getTitle());
ImageView img = (ImageView) view.findViewById(R.id.imageView);
img.setImageResource(R.drawable.dot_focus);
return view;
}
布局文件tab_item.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:text="text"
android:id="@+id/textView"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp" />
<ImageView
android:src="@drawable/dot_focus"
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
</LinearLayout>
NewsDetailActivity
/**
* @author: Hashub
* @WeChat: NGSHMVP
* @Date: 2018/9/5 16:37
* @function:新闻浏览页面
*/
public class NewsDetailActivity extends Activity implements View.OnClickListener
{
private TextView tvTitle;
private ImageButton ibMenu;
private ImageButton ibBack;
private ImageButton ibTextsize;
private ImageButton ibShare;
private WebView webview;
private ProgressBar pbLoading;
private String url;
private WebSettings webSettings;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detail);
findViews();
getData();
}
/**
* Find the Views in the layout<br />
* <br />
* Auto-created on 2016-08-24 09:43:47 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews()
{
tvTitle = (TextView) findViewById(R.id.tv_title);
ibMenu = (ImageButton) findViewById(R.id.ib_menu);
ibBack = (ImageButton) findViewById(R.id.ib_back);
ibTextsize = (ImageButton) findViewById(R.id.ib_textsize);
ibShare = (ImageButton) findViewById(R.id.ib_share);
webview = (WebView) findViewById(R.id.webview);
pbLoading = (ProgressBar) findViewById(R.id.pb_loading);
tvTitle.setVisibility(View.GONE);
ibMenu.setVisibility(View.GONE);
ibBack.setVisibility(View.VISIBLE);
ibTextsize.setVisibility(View.VISIBLE);
ibShare.setVisibility(View.VISIBLE);
ibBack.setOnClickListener(this);
ibTextsize.setOnClickListener(this);
ibShare.setOnClickListener(this);
}
/**
* Handle button click events<br />
* <br />
* Auto-created on 2016-08-24 09:43:47 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
@Override
public void onClick(View v)
{
if (v == ibBack)
{
// Handle clicks for ibBack
finish();
}
else if (v == ibTextsize)
{
// Handle clicks for ibTextsize
// Toast.makeText(NewsDetailActivity.this, "设置文字大小", Toast.LENGTH_SHORT).show();
showChangeTextSizeDialog();
}
else if (v == ibShare)
{
// Handle clicks for ibShare
// Toast.makeText(NewsDetailActivity.this, "分享", Toast.LENGTH_SHORT).show();
//showShare();
}
}
private int tempSize = 2;
private int realSize = tempSize;
private void showChangeTextSizeDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("设置文字大小");
String[] items = {"超大字体", "大字体", "正常字体", "小字体", "超小字体"};
builder.setSingleChoiceItems(items, realSize, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
tempSize = which;
}
});
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
realSize = tempSize;
changeTextSize(realSize);
}
});
builder.show();
}
private void changeTextSize(int realSize)
{
switch (realSize)
{
case 0://超大字体
webSettings.setTextZoom(200);
break;
case 1://大字体
webSettings.setTextZoom(150);
break;
case 2://正常字体
webSettings.setTextZoom(100);
break;
case 3://小字体
webSettings.setTextZoom(75);
break;
case 4://超小字体
webSettings.setTextZoom(50);
break;
}
}
private void getData()
{
url = getIntent().getStringExtra("url");
//设置支持javaScript
webSettings = webview.getSettings();
//设置支持javaScript
webSettings.setJavaScriptEnabled(true);
//设置双击变大变小
webSettings.setUseWideViewPort(true);
//增加缩放按钮
webSettings.setBuiltInZoomControls(true);
//设置文字大小
// webSettings.setTextSize(WebSettings.TextSize.NORMAL);
webSettings.setTextZoom(100);
//不让从当前网页跳转到系统的浏览器中
webview.setWebViewClient(new WebViewClient()
{
//当加载页面完成的时候回调
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
pbLoading.setVisibility(View.GONE);
}
});
webview.loadUrl(url);
// webview.loadUrl("http://www.atguigu.com/teacher.shtml");
}
}
网友评论