产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习。

弯路:

1.刚开始本来用PopupWindow去实现,做着之后发现如果用popupwindow实现的话,从标题栏下面弹出就比较麻烦.

2.最外层的布局本来是用LinearLayout去实现的,然后标题栏跟弹出的那个TextView外边包裹一层RelativeLayout,这样就会有一个问题,父布局RelativeLayout高度就是标题栏高度,提示框的高度最多也是标题栏高度,就算往下移动我们也看不到.

3.最外层布局改成RelativeLayout之后显示循序也是有秩序的,第一个是内容View,第二个是提示的View,第三个是标题栏的View.   android绘制View的原理就是最新画上去的一定在最上面,我们要保证标题栏始终显示,提示框可以遮住内容,内容是最后面的,所以需要第一个绘制。

实现步骤:

1.把显示提示View封装成自定义控件,继承自TextView.提供两个公共方法供外部调用.封装View的目的是代码复用,如果在其他地方也需要这种显示效果,在布局文件中引用自定义

View即可

1).showTips  显示提示View,调用向下移动动画,移动完成后延时一秒,再向上移动,并且改变透明度,动画结束后隐藏View。

2).setTitleHeight  看名字就知道设置标题栏的高度,因为我们这边是从标题栏下面弹出,所以我们得计算标题栏的高度是多少,才能知道往下面移动多少合适.移动上去同理.

2.Activity中显示自定义View.调用自定义View的公共方法.

1).在activity中注册回调接口来获取标题栏的高度,然后赋值给自定义View,

2).当我们需要提示的时候调用自定View的公共方法就行.

效果图如下:

ViewTestActivity.java     主Activity,程序的入口.

/**
*
* @author ansen
* @create time 2015-10-20
*/
public class ViewTestActivity extends Activity implements OnGlobalLayoutListener{
private TipTextView tvTips;//提示
private TextView tvTitle;//标题 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test); tvTips=(TipTextView) findViewById(R.id.tv_tips);
tvTitle=(TextView) findViewById(R.id.tv_title); //tvTitle在一个视图树中的焦点状态发生改变时,注册回调接口来获取标题栏的高度
ViewTreeObserver vto = tvTitle.getViewTreeObserver();
vto.addOnGlobalLayoutListener(this); findViewById(R.id.btn_show_tip).setOnClickListener(clickListener);
} private OnClickListener clickListener=new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_show_tip://显示提示
tvTips.showTips();
break;
}
}
}; @SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
tvTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);//删除监听
tvTips.setTitleHeight(tvTitle.getHeight());//把标题栏的高度赋值给自定义的TextView
}
}

activity_test.xml  显示的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <View
android:layout_above="@+id/btn_show_tip"
android:layout_below="@+id/tv_title"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#00FFFF"/> <com.example.tips.view.TipTextView
android:id="@+id/tv_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#faf3a1"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#ff4e00"
android:text="用动画实现提示"
android:visibility="gone"/> <TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="这是一个标题栏"
android:background="#FF00FF"
android:textSize="18sp"/> <Button
android:id="@+id/btn_show_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="弹窗提示!!!"/> </RelativeLayout>

TipTextView.java  自定义View,继承TextView

/**
* 自定义TextView 显示提示信息,显示时有动画效果(从上面弹出,然后改变透明度慢慢隐藏)
* @author ansen
* @create time 2015-10-20
*/
public class TipTextView extends TextView{
private static final int START_TIME=400;//动画显示时间
private static final int END_TIME=400;//动画移出时间
private static final int SHOW_TIME=1000;//动画显示时间 private int titleHeight=100;//标题栏默认的高度设置成100 public TipTextView(Context context) {
super(context);
} public TipTextView(Context context, AttributeSet paramAttributeSet) {
super(context, paramAttributeSet);
} public TipTextView(Context context, AttributeSet paramAttributeSet,int paramInt) {
super(context, paramAttributeSet, paramInt);
} public void showTips(){
setVisibility(View.VISIBLE); //向下移动动画
TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,0,titleHeight);
downTranslateAnimation.setDuration(START_TIME);
downTranslateAnimation.setFillAfter(true); startAnimation(downTranslateAnimation); //动画监听
downTranslateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation){//向下移动动画结束
topTranslateAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
} private void topTranslateAnimation(){
new Handler().postDelayed(new Runnable() {//延时1秒之后再向上移动
@Override
public void run(){ //向上移动动画
TranslateAnimation topTranslateAnimation=new TranslateAnimation(0,0,titleHeight,0);
topTranslateAnimation.setDuration(END_TIME);
topTranslateAnimation.setFillAfter(true); //改变透明度
AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);
alphaAnimation.setDuration(END_TIME); //两个动画添加到动画集合中
AnimationSet animationSet=new AnimationSet(true);
animationSet.addAnimation(topTranslateAnimation);
animationSet.addAnimation(alphaAnimation); startAnimation(animationSet);//开启动画 animationSet.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation){//动画结束隐藏提示的TextView
setVisibility(View.GONE);
}
});
}
},SHOW_TIME);
} /**
* 设置标题栏高度
* @param titleHeight
*/
public void setTitleHeight(int titleHeight) {
this.titleHeight = titleHeight;
}
}

这篇就用TextView实现了,如果你所有的activity都需要加上这个提示,请阅读我的的下篇博客.android标题栏上面弹出提示框(二)
PopupWindow实现,带动画效果

以上有步骤说明,效果图,源码,相信大家都能看懂....推荐下自己创建的android QQ群:202928390   欢迎大家的加入.

点击下载源码

android标题栏下面弹出提示框(一) TextView实现,带动画效果的更多相关文章

  1. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  2. android常用的弹出提示框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...

  3. PHP弹出提示框并跳转到新页面即重定向到新页面

    本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下   这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...

  4. [转] 在Asp.net前台和后台弹出提示框

    一.在前台弹出提示框 1.点击"A"标记或者"控件按钮"弹出提示框 <asp:LinkButton ID="lbtnDel" runa ...

  5. SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框

    1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...

  6. 基于Jquery 简单实用的弹出提示框

    基于Jquery 简单实用的弹出提示框 引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间 ...

  7. iOS bug 之 H5 页面没有弹出提示框

    描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...

  8. C#自动关闭弹出提示框

    自动关闭弹出提示框(用一个小窗体显示提示信息):例如在一个form窗体中弹出自动关闭的提示框1.首先创建一个弹出提示信息的窗体 AutoCloseMassageBox,在里面拖一个lable控件,去掉 ...

  9. 关于winform窗体关闭时弹出提示框,选择否时窗体也关闭的问题

    在窗体中有FormClosing这个事件,这个事件是在窗体关闭时候运行的.如果要取消某个事件的操作,那么就在该事件中写上e.Cancel=true就能取消该事件,也就是不执行该事件.所以,你要在窗体关 ...

随机推荐

  1. Shader实例:溶解效果(Dissolve)

    效果: 图左:一道金光闪过,瞬间灰飞烟灭 图右:燃烧效果,先过渡到黄色,然后渐渐过渡到黑色,最后消失殆尽. 这是游戏中常见的效果,各位可以想想自己玩过的游戏. 手头正在玩的,梦三国手游,死亡的时候就是 ...

  2. winfrom自定义滚动条

    panel或图片什么的跟着鼠标走,这里panel自己可以加背景图或直接搞个图就行了.为了演示清楚,有个滚动条控件做对比,与自定义的同步. using System; using System.Coll ...

  3. SQL 查找重复项及批量修改数据成固定格式

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select   peopleId  from ...

  4. C代码实现栈

    # include <stdio.h> # include <malloc.h> # include <stdlib.h> //C语言实现栈 //结点 typede ...

  5. jGestures: jQuery的手势事件插件

    官网地址:http://jgestures.codeplex.com/文档版本号: v0.7,由neuedigitale编辑,2012年5月8日最新稳定版: jGestures v0.90 - sha ...

  6. SSH配置与讲解

    一.Struts 首先介绍Struts,在web项目中加入Struts的jar包,并在Web.xml中添加Struts的配置: <filter>       <filter-name ...

  7. CSS基础篇之选择符3

    border(边框) 如何用CSS调出边框 我们给p标签加一个边框试一下 p{ border:1px solid #ccc:/*这是缩写*/ } 第一个值是为边框的宽度 第二个值是为边框线样式为直线 ...

  8. Google分布式构建软件之四:分发构建结果

    注:本文英文原文在google开发者工具组的博客上[需要FQ],以下是我的翻译,欢迎转载,但请尊重作者版权,注名原文地址. 之前的文章,介绍了Google在分布式构建软件过程中,如何把构建过程分发到许 ...

  9. 如何使用Worktile进行敏捷项目开发管理

    Worktile在任务管理上采用了看板视图,非常适合进行敏捷项目开发管理.事实上,在开发Worktile的过程中,我们也是自产自销,使用Worktile管理Worktile本身的开发过程,在本文中跟大 ...

  10. 《App研发录》 源码

    第1章源码: 1.1 重新规划Android项目结构 1.1.zip 1.2 为Activity定义新的生命周期 1.2.zip 1.3 统一事件编程模型 1.3.zip 1.4 实体化编程 1.4. ...