Android 之 view提升:七 标题栏与通知栏
一 动态设置标题栏文字和隐藏显示
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
findViewById(R.id.btn4).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
getSupportActionBar().setTitle("btn1");
getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.abc_0));
break;
case R.id.btn2:
getSupportActionBar().setTitle("btn2");
//设置透明的标题栏,只有灰度值一列
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#55000000")));
break;
case R.id.btn3:
getSupportActionBar().hide();
break;
case R.id.btn4:
getSupportActionBar().show();
break;
}
}
二 使用RemoteViews 自定义通知栏视图
代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= 26)
{
//当sdk版本大于26
String id = "channel_1";
String description = "143";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(id, description, importance);
// channel.enableLights(true); // 提示灯
// channel.enableVibration(true);// 震动
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(MainActivity.this, id)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("折叠式通知")
.setContentText("This is a content text")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.mlayout);
// 使用RemoteViews创建自定义视图
notification.bigContentView = remoteViews; //指定展开视图
notification.contentView = remoteViews; //指定普通视图
manager.notify(1, notification);
}
else
{
//当sdk版本小于26
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
manager.notify(1,notification);
}
}
});
}
三 使用SnackBar提示框
代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = findViewById(R.id.layout);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// layout: 父view
Snackbar snackbar=Snackbar.make(layout, "这是最简单的SnackBar",Snackbar.LENGTH_INDEFINITE)
.setAction("单击显示提示窗口", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"一个时刻只能有唯一的Snackbar显示"
, Toast.LENGTH_LONG).show();
}
});
setSnackBarMessageText(snackbar,Color.GREEN);
snackbar.show();
}
});
}
public static void setSnackBarMessageText(Snackbar snackbar, int color){
View view = snackbar.getView();
view.setBackgroundColor(Color.parseColor("#770000ff"));
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)view;
View myView = LayoutInflater.from(view.getContext()).inflate(R.layout.mlayout,null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
// layout.addView(myView,1,params);
TextView textView = view.findViewById(R.id.snackbar_text);
textView.setTextColor(color);
textView.setTextSize(20);
Button btn = view.findViewById(R.id.snackbar_action);
btn.setTextColor(Color.parseColor("#FF000000"));
}
}