话不多说先上图:
apk下载:百度网盘下载 提取码:l8ry
用到知识点:
1.WebView布局,并引入html代码让拿到的数据里面的标签生效。
由于刚拿到的数据全是<p>所以显示在WebView布局会乱掉,要想标签生效、首行缩进就要引入html代码了,用法如下:
首先定义html代码:
private static final String WEBVIEW_CONTENT_NIGHT = "<html><head></head><body style=\"text-align:justify;margin:10;text-indent:2em; background: #313639;\">%s</body></html>";
private static final String WEBVIEW_CONTENT_LIGHT = "<html><head></head><body style=\"text-align:justify;margin:10;text-indent:2em; background: #ffffff;\">%s</body></html>";
然后拿到数据并渲染:
wv.loadDataWithBaseURL(null, String.format(WEBVIEW_CONTENT_NIGHT, obj2.getString("content")), "text/html", "utf-8", null);
2.上滑WebView隐藏多余布局。为节省阅读空间在上滑的时候我想要隐藏作者和字数,所以这里就要用OnScrollChangeListener,首先实现OnScrollChangeListener:
然后获取控件并实现监听:
wv = findViewById(R.id.wv);
wv.setOnScrollChangeListener(this);
最后在方法中设置纵向滑动多上距离隐藏布局:
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > 100) {
rl.setVisibility(View.GONE);//隐藏作者
} else if (scrollY == 0) {
rl.setVisibility(View.VISIBLE);//显示作者
}
}
3.点击两次返回键退出程序。为防止误触,所以设置点击一次返回键提示再按一次退出程序,两次间隔时间两秒。方法如下:
private boolean mBackKeyPressed = false;//记录是否有首次按键
//点击两次退出程序
public void onBackPressed() {
if (!mBackKeyPressed) {
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {//延时两秒,如果超出则清除第一次记录
@Override
public void run() {
mBackKeyPressed = false;
}
}, 2000);
} else {
//结束Activity
finish();
}
}
4.还有一个是比较常用的全屏显示隐藏状态栏。(一定要加在setContentView()之上)
//去掉Activity上面的状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
5.底部弹窗并保存ToggleButton的按钮状态(这里用了0和1判断开和关也可用true和flase)根据状态判断是否为夜间模式。
dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
//填充对话框的布局
inflate = LayoutInflater.from(this).inflate(R.layout.dialog, null);
//获取控件
tb = inflate.findViewById(R.id.tb);//夜间
//获取监听
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
try {
wv.loadDataWithBaseURL(null, String.format(WEBVIEW_CONTENT_NIGHT, obj2.getString("content")), "text/html", "utf-8", null);
sprfMain = getSharedPreferences("counter", Context.MODE_PRIVATE);
editorMain = sprfMain.edit();
editorMain.putInt("bg", 0);
editorMain.commit();
ll.setBackgroundResource(R.color.colorNight);
v_x.setBackgroundResource(R.color.colorNight);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
try {
wv.loadDataWithBaseURL(null, String.format(WEBVIEW_CONTENT_LIGHT, obj2.getString("content")), "text/html", "utf-8", null);
sprfMain = getSharedPreferences("counter", Context.MODE_PRIVATE);
editorMain = sprfMain.edit();
editorMain.putInt("bg", 1);
editorMain.commit();
ll.setBackgroundResource(R.color.colorWhite);
v_x.setBackgroundResource(R.color.colorLightGray);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
//取出保存的值
sprfMain = getSharedPreferences("counter", Context.MODE_PRIVATE);
bg = sprfMain.getInt("bg", 1);
if (bg == 0) {
tb.setChecked(true);
} else if (bg == 1) {
tb.setChecked(false);
}
//将布局设置给Dialog
dialog.setContentView(inflate);
//获取当前Activity所在的窗体
Window dialogWindow = dialog.getWindow();
//设置Dialog从窗体底部弹出
dialogWindow.setGravity(Gravity.BOTTOM);
//获得窗体的属性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.y = 0;//设置Dialog距离底部的距离
//宽度填满
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
//将属性设置给窗体
dialogWindow.setAttributes(lp);
dialog.show();//显示对话框
ActionSheetDialogStyle样式文件和弹出动画:
<style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<!-- 浮于Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- Dialog以外的区域模糊效果 -->
<item name="android:backgroundDimEnabled">false</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- Dialog进入及退出动画 -->
<item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item>
</style>
<!-- ActionSheet进出动画 -->
<style name="ActionSheetDialogAnimation" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>
<item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>
</style>
actionsheet_dialog_in:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromYDelta="100%"
android:toYDelta="0" />
actionsheet_dialog_out:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromYDelta="0"
android:toYDelta="100%" />
还有dialog布局:(根据自己的布局修改)
<?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"
android:background="@color/colorWhite"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center"
android:text="夜间" />
<ToggleButton
android:id="@+id/tb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:button="@drawable/btn_backgrounds"
android:textOff="关"
android:textOn="开" />
</RelativeLayout>
</LinearLayout>
最后根据保存的数据判断并显示是否为夜间模式:
//取出保存的值
sprfMain = getSharedPreferences("counter", Context.MODE_PRIVATE);
bg = sprfMain.getInt("bg", 1);
if (bg == 0) {
wv.loadDataWithBaseURL(null, String.format(WEBVIEW_CONTENT_NIGHT, obj2.getString("content")), "text/html", "utf-8", null);
ll.setBackgroundResource(R.color.colorNight);
v_x.setBackgroundResource(R.color.colorNight);
} else if (bg == 1) {
wv.loadDataWithBaseURL(null, String.format(WEBVIEW_CONTENT_LIGHT, obj2.getString("content")), "text/html", "utf-8", null);
ll.setBackgroundResource(R.color.colorWhite);
v_x.setBackgroundResource(R.color.colorLightGray);
}
详细代码请到GitHub:GitHub - cuiwenju2017/EveryDayIs: 每日一文