anndroid 模糊引导界面
时间:2022-04-26
本文章向大家介绍anndroid 模糊引导界面,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
先上两张图,后面补上代码
我们以前的写法是在需要显示模糊引导的地方,写一个布局,然后第一次使用的时候显示出来。但是这样做代码结构不清晰,所以我们有必要将这些View独立出来,写成一个自定义的View
public class ShowMemberTipsView extends BaseTipsView {
@Bind(R.id.btn_sure)
Button btnSure;
@Bind(R.id.img_close)
ImageView imgClose;
private static final String UNIQUE_KEY = ShowMemberTipsView.class.getSimpleName();
private Context mContext;
public ShowMemberTipsView(Context context) {
super(context);
this.mContext = context;
init();
}
public ShowMemberTipsView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
}
public ShowMemberTipsView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
init();
}
@Override
public String getUniquekey() {
return UNIQUE_KEY;
}
private void init() {
initLayoutParams();
addStatusBarView();
addContentView();
}
private void initLayoutParams() {
LayoutParams lp = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
setOrientation(VERTICAL);
setLayoutParams(lp);
}
private void addContentView() {
View view = inflate(mContext, R.layout.view_show_member_tips, null);
ButterKnife.bind(this,view);
addView(view);
}
@OnClick(R.id.btn_sure)
public void sure() {
if (mOnSureListener != null) {
mOnSureListener.onSure(ShowMemberTipsView.this);
}
}
@OnClick(R.id.img_close)
public void close() {
if (mOnCloseListener != null) {
mOnCloseListener.onClose(ShowMemberTipsView.this);
}
}
}
引用的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/topbar_layout_width"
android:layout_marginTop="120dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1.0">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/icon_more_mask_btn" />
</RelativeLayout>
<View
android:layout_width="1.0px"
android:layout_height="fill_parent"
android:layout_marginBottom="10.0dip"
android:layout_marginTop="10.0dip" />
<View
android:id="@+id/rl_task_center"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:src="@drawable/icon_more_mask_text" />
<Button
android:id="@+id/btn_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@drawable/icon_more_mask_level_btn" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<ImageView
android:id="@+id/img_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:padding="5dp"
android:src="@drawable/icon_app_upgrade_close" />
</FrameLayout>
最后是一个BaseView,主要是封装一些公共的方法
public abstract class BaseTipsView extends LinearLayout {
private static final String TAG = BaseTipsView.class.getSimpleName();
private String mUniqueKey;
protected OnCloseListener mOnCloseListener;
protected OnSureListener mOnSureListener;
public BaseTipsView(Context context) {
super(context);
init();
}
public BaseTipsView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BaseTipsView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mUniqueKey = getUniquekey();
if (TextUtils.isEmpty(mUniqueKey)) {
throw new IllegalArgumentException("Uniquekey must not empty!");
}
maskClick();
}
//获取状态
protected void addStatusBarView() {
View statusBarView = new View(getContext());
statusBarView.setBackgroundColor(getContext().getResources().getColor(R.color.line_color));
int statusBarHeight = getStatusBarHeight();
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
statusBarView.setLayoutParams(lp);
addView(statusBarView);
}
public abstract String getUniquekey();
private void maskClick() {
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 因此View背景透明,防止点击到此View后面的控件
}
});
}
public void show(final Activity activity) {
if (!isMoreTipsShowed()) {
addViewToDector(activity);
setMoreTipsShowed(true);
}
}
public void dismiss(final Activity activity) {
removeViewFromDector(activity);
}
/**
* 添加View到dectorView
* @param activity
*/
private void addViewToDector(final Activity activity) {
ViewGroup dectorView = (ViewGroup) activity.getWindow().getDecorView();
dectorView.addView(this);
}
/**
* 从dectorView移出View
* @param activity
*/
private void removeViewFromDector(final Activity activity) {
ViewGroup dectorView = (ViewGroup) activity.getWindow().getDecorView();
dectorView.removeView(this);
}
public void setOnCloseListener(OnCloseListener onCloseListener) {
this.mOnCloseListener = onCloseListener;
}
public void setOnSureListener(OnSureListener onSureListener) {
this.mOnSureListener = onSureListener;
}
public interface OnCloseListener {
public void onClose(BaseTipsView baseTipsView);
}
public interface OnSureListener {
public void onSure(BaseTipsView baseTipsView);
}
protected boolean isMoreTipsShowed() {
SharedPreferences sp = getContext().getSharedPreferences(TAG, Context.MODE_PRIVATE);
return sp.getBoolean(mUniqueKey, false);
}
protected void setMoreTipsShowed(boolean isShowed) {
SharedPreferences sp = getContext().getSharedPreferences(TAG, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(mUniqueKey, isShowed);
editor.commit();
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
最后附上源码下载地址:http://download.csdn.net/detail/xiangzhihong8/9549873
- SpringCloud注册中心集群搭建
- SpringCloud配置中心集群搭建
- HDU1846 Brave Game
- 拉格朗日插值
- python爬虫入门(二)Opener和Requests
- python爬虫入门(三)XPATH和BeautifulSoup4
- python爬虫入门(四)利用多线程爬虫
- LOJ #115. 无源汇有上下界可行流
- 数据库改名系列(数据库名,逻辑名,物理文件名)
- BZOJ1468: Tree
- 洛谷P3806 【模板】点分治1
- 探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)
- 洛谷P3383 【模板】线性筛素数(Miller_Rabin)
- BZOJ3667: Rabin-Miller算法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Go语言入门(五)结构体&练习
- 利用hexo和github或coding 搭免费个人博客
- window 指令之 tree
- Go语言入门(六)结构体后续&指针
- 一天一大 leet(二叉树的序列化与反序列化)难度:困难 DAY-16
- 一天一大 leet(三数之和)难度:中等 DAY-12
- MongoDB Docker版本:基础入门和复制集
- Django连接MySql使用models处理数据
- 一天一大 leet(爬楼梯)难度:简单 DAY-13
- 一天一大 leet(最长公共前缀)难度:简单 DAY-15
- Go语言入门(七)goroutine和channel
- 一天一大 leet(从先序遍历还原二叉树)难度:困难 DAY-18
- Go语言入门(八)线程安全&锁
- 一天一大 leet(最佳观光组合)难度:中等 DAY-17
- Django环境搭建