Android下Notification,样式style,主题theme的功能实现

一:Notification

1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2. notification.setLatestEventInfo(this, "国安部给你发短信!", "你被通辑啦~~~", contentIntent);

  2.1: PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

2.1.1: Intent intent = new Intent();

  intent.setAction(Intent.ACTION_CALL);

 intent.setData(Uri.parse("tel:110"));

3.nm.notify(0,notification);

3.1:Notification notification = new

 Notification(R.drawable.ic_launcher,"有新的消息到来了",System.currentTimeMillis());

4.为避免成为流氓软件,要: notification.flags = Notification.FLAG_AUTO_CANCEL;

详细代码:

 

  1. public class MainActivity extends Activity {  
  2.   
  3.     private NotificationManager nm;  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  8.     }  
  9.     // 通知是显示在一个系统应用里面的, 如果系统应用ui挂了,通知就不会出来了。  
  10.     public void click(View view) {  
  11.          @SuppressWarnings("deprecation")  
  12.          Notification notification = new  
  13.          Notification(R.drawable.ic_launcher,"有新的消息到来了",System.currentTimeMillis());           
  14.          notification.flags = Notification.FLAG_AUTO_CANCEL;           
  15. //       //延期的意图,不是立刻执行的意图,最终这个意图是在另外一个应用程序里面执行  
  16.          Intent intent = new Intent();  
  17.          intent.setAction(Intent.ACTION_CALL);  
  18.          intent.setData(Uri.parse("tel:110"));         
  19.          PendingIntent contentIntent = PendingIntent.getActivity(this0, intent, 0);          
  20.          //旧的api,一定要记得设置notification这一个参数  
  21.          notification.setLatestEventInfo(this"国安部给你发短信!""你被通辑啦~~~", contentIntent);   
  22.          nm.notify(0,notification);  
  23.       }  
  24.   }  


 

运行结果:

 

二:样式:style(只能作用在控件上)

1.在res/values/styles.xml设置样式

 <style name="text_content_style">

        <!-- item里面是键值对! -->

        <item name="android:layout_width">wrap_content</item>

        <item name="android:layout_height">wrap_content</item>

        <item name="android:textColor">#FF0000</item>

        <item name="android:textSize">20sp</item>

    </style>

    

   继承形式一 

   <style name="text_title_style" parent="@style/text_content_style">

       <item name="android:textSize">30sp</item>

   </style>

   

   继承形式二 

   <style name="text_subtitle_style" parent="@style/text_content_style">

       <item name="android:textSize">25sp</item>

   </style>

2.使用样式:

    <TextView 

        style="@style/text_title_style"

        android:text="我是标题"/>

    <TextView 

        style="@style/text_subtitle_style"

        android:text="我是sub标题"/>

    <TextView 

        style="@style/text_content_style"

        android:text="我是文本"/>

3.效果图:

 

三:主题theme(作用在Activity或整个应用程序上Application)

1.在style.xml中配置:

参考:~\adt-bundle-windows-x86-20130729\sdk\platforms\android-16\data\res\values\themes.xml

第162行:Window attributes,可配置自己的Window attribute的主题。

注意:不同于系统的,自己要定义需要在name的字符串前加上:android:   如:

系统:

<item name="windowBackground">@android:drawable/screen_background_selector_dark</item>

自己:

 <item name="android:windowBackground">@color/red</item>

 

    <style name="red_theme">

        <item name="android:windowBackground">@color/red</item>

</style>

 

2.在AndroidMainfest.xml中使用:

 <activity

            android:theme="@style/red_theme"

...

 

效果图:

转载于:https://www.cnblogs.com/android100/p/Android-Notification-style-theme.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值