Android移动应用开发之Toolbar、AlertDialog、popupWindow的简单使用

Toolbar

这个就是用来自定义手机页面上面的那一条导航栏的
因为默认配置是有一个导航栏的,但是我们需要用自己写的话就需要改一下配置文件:
在这里插入图片描述
将原来默认的

    <style name="Theme.Hunter" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

改为

    <style name="Theme.Hunter" parent="Theme.MaterialComponents.DayNight.NoActionBar">

就能够使用自定义的toolbar了

布局文件中写一下toolbar:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffff00"
        app:logo="@drawable/ic_baseline_account_box_24"
        app:navigationIcon="@drawable/left"
        app:subtitle="子标题"
        app:subtitleTextColor="#00ffff"
        app:title="标题"
        app:titleMarginStart="90dp"
        app:titleTextColor="#ffff0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="20sp"
            android:text="标题2" />
    </androidx.appcompat.widget.Toolbar>


</LinearLayout>

可以看一下效果
在这里插入图片描述
蛮好的。

AlertDialog

这个就是弹出对话框的。

主要文件目录

在这里插入图片描述

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="hunterClick"
        android:text="显示对话框"
        />


</LinearLayout>

dialog_view.xml

用于放在对话框里的界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#ffff00"
    android:orientation="vertical">

    <ImageView
        android:src="@drawable/ic_baseline_account_box_24"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:text="今天天气很好" />
</LinearLayout>

MainActivity

package zufe.scq.hunter;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      }
    public void hunterClick(View view){
        View dialog_view = getLayoutInflater().inflate(R.layout.dialog_view, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_baseline_account_box_24)
                .setTitle("我是对话框")
                .setMessage("今天天气怎么样啊")
                .setView(dialog_view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("TAG", "点击了确定");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("TAG", "点击了取消");
                    }
                })
                .setNeutralButton("中间", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("TAG", "点击了中间");
                    }
                })
                .create()
                .show();
      }

}

点击运行
在这里插入图片描述
点击按钮:
在这里插入图片描述

点击里面的按钮会出现对应的打印:
在这里插入图片描述
说明按钮设置成功。

那么一个简单的对话框就完成了。

popupWindow

实现弹窗功能

主要文件目录

在这里插入图片描述

activity_main.xml

主界面是一个按钮,点击得到弹窗

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="hunterClick"
        android:text="显示对话框"
        />


</LinearLayout>

pop_view.xml

弹窗出来两个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:padding="5dp"
        android:text="上海"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn2"
        android:padding="5dp"
        android:text="北京"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity

package zufe.scq.hunter;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      }
    public void hunterClick(View view){
        View pop_view = getLayoutInflater().inflate(R.layout.pop_view, null);
        Button btn1 = pop_view.findViewById(R.id.btn1);
        Button btn2 = pop_view.findViewById(R.id.btn2);
        PopupWindow popupWindow = new PopupWindow(pop_view,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,
                true);
//      设置背景图
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.a));
        popupWindow.showAsDropDown(view, 10, 10);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("TAG", "你是住在上海吗");
                popupWindow.dismiss();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("TAG", "你是住在北京吗");
                popupWindow.dismiss();
            }
        });

      }

}

运行
在这里插入图片描述
点击按钮
在这里插入图片描述

点击上海
在这里插入图片描述
弹窗消失

在这里插入图片描述
控制台输出
按下北京按钮亦是如此。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Icy Hunter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值