Android 之 view提升:七 标题栏与通知栏

本文介绍了如何在Android应用中动态设置标题栏文字和隐藏显示,使用RemoteViews实现自定义通知栏视图,以及通过SnackBar创建提示框的代码示例。
摘要由CSDN通过智能技术生成

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"));
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值