Android原生的Toast功能单一,仅仅够日常开发需要,若打算实现生动有趣的消息提示通知,则可以考虑使用Android上第三方开源的项目:NiftyNotification

NiftyNotification的效果如图所示(部分):


NiftyNotification在github上的项目主页是:https://github.com/sd6352051/NiftyNotification
NiftyNotification本身又依赖于另外一个github上的第三方开源项目NineOldAndroids,NineOldAndroids在github上的项目主页是:https://github.com/JakeWharton/NineOldAndroids
正确添加NineOldAndroids引用后,即可直接使用NiftyNotification。简单期间,甚至可以直接将NiftyNotification的单个jar包下载后加入到自己的项目libs中,然后直接使用。
NiftyNotification无需配置xml文件,只需像Android原生的Toast那样写上层Java代码即可,NiftyNotification的Java代码写法简单,可设置的参数丰富,可定制性强,摘录NiftyNotification项目中的部分Java代码示例:


package com.example.niftynotification_master;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.Toast;import com.gitonway.lee.niftynotification.lib.Configuration;import com.gitonway.lee.niftynotification.lib.Effects;import com.gitonway.lee.niftynotification.lib.NiftyNotificationView;public class MainActivity extends Activity {private Effects effect;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void showNotify(View v) {String msg = "Today we’d like to share a couple of simple styles and effects for android notifications.";switch (v.getId()) {case R.id.scale:effect = Effects.scale;break;case R.id.thumb_slider:effect = Effects.thumbSlider;break;case R.id.jelly:effect = Effects.jelly;break;case R.id.slide_in:effect = Effects.slideIn;break;case R.id.flip:effect = Effects.flip;break;case R.id.slide_on_top:effect = Effects.slideOnTop;break;case R.id.standard:effect = Effects.standard;break;}NiftyNotificationView.build(this, msg, effect, R.id.mLyout).setIcon(R.drawable.lion) // You// must// call// this// method// if// you// use// ThumbSlider// effect.show();// You can configure like this// The default// Configuration cfg=new Configuration.Builder()// .setAnimDuration(700)// .setDispalyDuration(1500)// .setBackgroundColor("#FFBDC3C7")// .setTextColor("#FF444444")// .setIconBackgroundColor("#FFFFFFFF")// .setTextPadding(5) //dp// .setViewHeight(48) //dp// .setTextLines(2) //You had better use setViewHeight and setTextLines// together// .setTextGravity(Gravity.CENTER) //only text def// Gravity.CENTER,contain icon Gravity.CENTER_VERTICAL// .build();//// NiftyNotificationView.build(this,msg, effect,R.id.mLyout,cfg)// .setIcon(R.drawable.lion) //remove this line ,only text// .setOnClickListener(new View.OnClickListener() {// @Override// public void onClick(View view) {// //add your code// }// })// .show(); // show(boolean) allow duplicates or showSticky() sticky// notification,you can call removeSticky() method close it}}

需要的activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#34495E"    android:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:id="@+id/title"        android:layout_width="match_parent"        android:layout_height="48dp"        android:background="#000000"        android:gravity="center"        android:text="Nifty Modal Notification Effects"        android:textColor="#FFFFFF" />    <ScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/title"        android:scrollbars="none" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            android:padding="5dp" >            <Button                android:id="@+id/scale"                style="@style/my_btn"                android:text="SCALE" />            <Button                android:id="@+id/thumb_slider"                style="@style/my_btn"                android:text="THUMB SLIDER" />            <Button                android:id="@+id/jelly"                style="@style/my_btn"                android:text="JELLY" />            <Button                android:id="@+id/slide_in"                style="@style/my_btn"                android:text="SLIDE IN" />            <Button                android:id="@+id/flip"                style="@style/my_btn"                android:text="FLIP" />            <Button                android:id="@+id/slide_on_top"                style="@style/my_btn"                android:text="SLIDE ON TOP" />            <Button                android:id="@+id/standard"                style="@style/my_btn"                android:text="STANDARD" />        </LinearLayout>    </ScrollView>    <RelativeLayout        android:id="@+id/mLyout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/title"        android:clipChildren="true" >    </RelativeLayout></RelativeLayout>

需要的styles,xml:

<resources>    <!-- Base application theme. -->    <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">        <!-- Customize your theme here. -->    </style>    <style name="AppTheme" parent="AppBaseTheme">        <!-- Customize your theme here. -->    </style>    <style name="my_btn">        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:background">#44000000</item>        <item name="android:padding">15dp</item>        <item name="android:layout_margin">5dp</item>        <item name="android:textColor">#FFFFFF</item>        <item name="android:onClick">showNotify</item>        <item name="android:textSize">20sp</item>    </style></resources>


更多相关文章

  1. 解决用Android Studio开发项目如何放置assets文件夹
  2. Android优秀开源组件项目集合
  3. 高手速成android开源项目【项目篇】
  4. 【转】android开源项目---项目篇
  5. 高手速成android开源项目【developer篇】
  6. Android代码设置EditText只输入数字、字母
  7. google android 项目
  8. UE4打包安卓项目遇到UATHelper: Packaging (Android (ETC1)): ER
  9. [Android][Audio] audio_policy.conf文件分析

随机推荐

  1. python-初学爬虫
  2. haploview使用介绍
  3. gin(6)-模板渲染
  4. 基于Kubernetes部署Jenkins动态构建环境
  5. 前端小白学习web缓存笔记
  6. HTML小白入门2:html常用的标签
  7. 10.0 Ansible Docker
  8. flea-frame-db使用之JPA封装介绍
  9. Transformer稀疏模型:​将文本、图像、声
  10. 隐马尔科夫模型-前向算法