Android进阶之光——Android5.0新特性
2021-08-10 本文已影响0人
So_ProbuING
Android5.0新特性
主要新特性概述
- 全新的Meterial Design风格
- 支持多种设备
- 全新的通知中心设计
- 支持64位ART虚拟机
- Android5.0中,放弃了之前一直使用的Dalvik虚拟机,改用ART虚拟机
- Overview 多任务视窗
- 设备识别解锁
- Ok Google语音指令
- Face unlock 面部解锁
替换ListView、GridView的RecyclerView
CardView 卡片样式组件
3种Notification
- 普通Notification
- 折叠式Notification
在折叠式的Notification中,我们需要自定义视图,而这个视图显式的进程和我们创建视图的进程不在一个进程,所以我们需要使用RemoteViews来创建我们的自定义视图
下面我们来试着写一个折叠式的Notification- 布局文件
- view_fold.xml
- 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="展开后的视图"
android:textColor="@color/design_default_color_primary_dark" />
</LinearLayout>
- java代码
private void showFoldNotity() {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("AndroidLight", "showFoldNotity");
//使用RemoteView创建自定义视图
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getPackageName());
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.ic_launcher_m);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
builder.setAutoCancel(true);
builder.setContentTitle("悬挂式");
builder.setFullScreenIntent(pendingIntent, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(getPackageName());
NotificationChannel channel = new NotificationChannel(
this.getPackageName(),
"会话消息(掌嗨)",
NotificationManager.IMPORTANCE_HIGH
);
notificationManager.createNotificationChannel(channel);
}
Notification notification = builder.build();
//折叠式
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
notification.bigContentView = remoteViews;
notification.contentView = remoteViews;
notificationManager.notify(1, notification);
}
- 悬挂式Notification
builder.setFullScreenIntent(pendingIntent, true);
Toolbar与Palette
Toolbar是应用内容的标准工具栏,也可以说是Actionbar的升级版。
引入Toolbar
引入v7包
修改style
将style文件中的Actionbar去掉
<style name="Theme.AndroidLight" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
Toolbar各个部分属性
- textColorPrimary 标题颜色
- colorPrimary 标题栏颜色
- colorPrimaryDark 状态栏颜色
- windowBackground 窗口背景颜色
-
navigationBarColor 导航栏背景颜色
image.png
定义toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
>
</androidx.appcompat.widget.Toolbar>
我们使用一个侧滑效果
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ToolBarDrawerLayoutActivity">
<include layout="@layout/mytoolbar" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/id_drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="内容界面"
android:textColor="@android:color/black" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/darker_gray"
android:orientation="vertical">
<TextView
android:id="@+id/tv_close"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:text="侧滑界面,点击收回"
android:textColor="@android:color/black" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
在Activity中设定Toolbar
public class ToolBarDrawerLayoutActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar_drawer_layout);
initViews();
}
private void initViews() {
mToolbar = ((Toolbar) findViewById(R.id.toolbar));
setSupportActionBar(mToolbar);
}
}
-
运行效果
image.png
自定义Toolbar
- 创建一个menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ToolbarActivity" >
<item
android:id="@+id/ab_search"
android:orderInCategory="80"
android:title="搜索"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_share"
android:orderInCategory="90"
android:title="分享"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="设置"
app:showAsAction="never"/>
</menu>
覆写onCreateOptionsMenu,并在toolbar的setOnMenuItemClickListener实现点击回调
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
toolbar.setOnMenuItemClickListener实现点击Menu回调
private void initViews() {
tv_close = ((TextView) findViewById(R.id.tv_close));
mToolbar = ((Toolbar) findViewById(R.id.toolbar));
mToolbar.setTitle("ToolBar");
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //设置返回值图标
getSupportActionBar().setLogo(R.drawable.ic_launcher);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(ToolBarDrawerLayoutActivity.this, "setting", Toast.LENGTH_SHORT).show();
break;
case R.id.ab_share:
Toast.makeText(ToolBarDrawerLayoutActivity.this, "ab_share", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}
添加DrawerLayout实现侧滑
mDrawerLayout = ((DrawerLayout) findViewById(R.id.id_drawerlayout));
//侧划开关绑定
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
actionBarDrawerToggle.syncState();
mDrawerLayout.addDrawerListener(actionBarDrawerToggle);
tv_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
});
Palette应用
Android 5.x使用Palette来提取颜色,从而让主题能够动态适应当前界面的色调