Android开发之UI控件

news/2024/5/17 17:48:24 标签: android, ui, gitee

TextView

实现阴影效果的textview

  • android:shadowColor="#ffff0000" 设置阴影颜色为红色
  • android:shadowRadius="3" 设置阴影的模糊程度为3
  • android:shadowDx="10" 设置阴影在水平方向的偏移
  • android:shadowDy="10" 设置阴影在竖直方向的偏移
    效果
    实现跑马灯效果的textview
  • android:singleLine="true" 内容单行显示
  • android:ellipsize="marquee" 在文本过长而无法完全显示时,让文本滚动显示
  • android:marqueeRepeatLimit="marquee_forever"设置文本的重复次数
  • android:focusable="true"设置TextView是否可以接收焦点
  • android:focusableInTouchMode="true"设置TextView在触摸模式下是否可以接收焦点
  • android:clickable="true"设置TextView是否可以被点击(设置后点击文本开始滚动,如果想要实现进入应用自动滚动可以继承textview重写isFocused方法,并把控件部分更改为自定义控件,如com.example.myapplication.myTextView)

ProgressBar

  • style="?android:attr/progressBarStyleHorizontal" 水平进度条
  • android:max="100" 进度条最大值
  • android:indeterminate="true" 如果设置为true,则进度条不精确显示进度
  • android:progress进度条已完成进度值
    效果
    activity中设置点击一次进度值+10
  fun loadOnclick(view: View) {
        var progress = loadprogressBar.progress
        progress+=10
        loadprogressBar.progress = progress
    }

Notification对话框

  • setContentTitle("软件通知")主题
  • setContentText("notification练习")内容
  • setSmallIcon(R.drawable. ) 小图标
  • setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable. ))大图标
  • setColor(Color.parseColor(“#ff0000”))小图标颜色
  • setContentIntent(pendingIntent)点击通知跳转的页面
  • setAutoCancel(true)点击通知后是否自动清除
    代码:
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  //获取NotificationManager对象(通知管理类)
        manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建NotificationChannel
            var channel = NotificationChannel(
                "MainActivity", "测试",
                //通知重要程度设置
                NotificationManager.IMPORTANCE_HIGH//开启通知,会弹出,发出提示音,状态栏中显示
            )

            manager.createNotificationChannel(channel)
        }

        var intent = Intent(this, MainActivity2::class.java)
        //将Intent转换成pendingIntent
        var pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)

        //Notification对象,设置通知内容和格式
        //channelid渠道id
        notification = NotificationCompat.Builder(this, "MainActivity")
            .setContentTitle("软件通知")//主题
            .setContentText("notification练习")//内容
            .setSmallIcon(R.drawable.baseline_campaign_24) //小图标
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.img))//大图标
            .setColor(Color.parseColor("#ff0000"))//小图标颜色
            .setContentIntent(pendingIntent)//点击通知跳转的页面
            .setAutoCancel(true)//点击通知后是否自动清除
            .build()
    }


 fun sendOnClick(view: View) {
        //发出通知
        manager.notify(1, notification)
    }

    fun cacelOnClick(view: View) {
        //取消通知
        manager.cancel(1)
    }

Toolbar

  • android:layout_width="match_parent"toolbar的宽度
  • android:layout_height="?attr/actionBarSize"toolbar的高度
  • android:background="#0000ff"toolbar的背景颜色设置
  • app:navigationIcon="@drawable/baseline_arrow_back_24"toolbar的图标
  • app:title="标题"toolbar的主标题
  • app:titleTextColor="#ff0000"toolbar的主标题颜色
  • app:titleMarginStart="40dp"toolbar的主标题距离左边的边距
  • app:subtitle="子标题"toolbar的子标题
  • app:subtitleTextColor="#00ff00"toolbar的子标题颜色
  • app:logo="@drawable/img"toolbar的logo
    效果
    Toolbar点击事件
    toolbar = findViewById(R.id.toolbar)
        toolbar.setNavigationOnClickListener(View.OnClickListener {
            Toast.makeText(this, "toolbar被点击了", Toast.LENGTH_SHORT).show()
        })

AlertDialog

  • var builder = AlertDialog.Builder(this)构建对话框参数
  • builder.setIcon(R.drawable.img)添加图标
  • builder.setTitle("对话框")添加对话框标题
  • builder.setMessage("今天过的开心吗")添加对话框消息内容
  • builder.setView(R.layout.dialog)添加对话框自定义布局
  • 设置确定按钮,我修改成我需要的了
builder.setPositiveButton("是的",DialogInterface.OnClickListener { dialogInterface, i ->
                Toast.makeText(this, "是的", Toast.LENGTH_SHORT).show()
            })
  • 设置取消按钮,我修改成我需要的了
builder.setNegativeButton("不是",DialogInterface.OnClickListener { dialogInterface, i ->
                Toast.makeText(this, "不是", Toast.LENGTH_SHORT).show()
            })
  • 设置中间按钮,我修改成我需要的了
builder.setNeutralButton("不好也不坏",DialogInterface.OnClickListener { dialogInterface, i ->
                Toast.makeText(this, "不好也不坏", Toast.LENGTH_SHORT).show()
            })
  • builder.create()创建对话框
  • builder.show()显示对话框

效果

PopupWindow

创建一个PopupWindow对象。
popupView是这个弹出窗口的内容视图。
ViewGroup.LayoutParams.WRAP_CONTENT表示弹出窗口的宽度和高度都是根据其内容自动调整的。
true表示当点击弹出窗口以外的空白区域时,弹出窗口会被关闭

var popupWindow = PopupWindow(
            popupView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true//点空白处退出弹窗
        )
  • setContentView(View contentView)设置popupwindow显示的view
  • popupWindow.showAsDropDown(view)相对某个控件的位置(正左下方)无偏移
  • showAsDropDown(View anchor, int xoff, int yoff)相对某个控件的位置,有偏移
  • popupWindow.setBackgroundDrawable(getDrawable(R.drawable.img))设置背景
  • setFocusable(boolean focusable)设置是否获取焦点(点空白处退出弹窗)
  • popupWindow.dismiss()关闭弹窗
  • setAnimationStyle(int animationStyle)设置加载动画
  • setTouchable(boolean touchable)设置弹窗内部触摸使能(处理触摸事件)
  • setOutsideTouchable(boolean touchable)设置弹窗外部触摸使能(处理触摸事件)
    全部代码
 fun popupOnClick(view: View) {
        var popupView = layoutInflater.inflate(R.layout.popupview, null)
        var btn1 =popupView.findViewById<Button>(R.id.button)
        var btn2 =popupView.findViewById<Button>(R.id.button2)
        var popupWindow = PopupWindow(
            popupView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true//点空白处退出弹窗
        )

        popupWindow.setBackgroundDrawable(getDrawable(R.drawable.img))

        popupWindow.showAsDropDown(view)//显示在按钮下方

		//弹窗中按钮点击事件
        btn1.setOnClickListener(View.OnClickListener {
            Toast.makeText(this, "点击了是的按钮", Toast.LENGTH_SHORT).show()
        })

        btn2.setOnClickListener(View.OnClickListener {
            Toast.makeText(this, "点击了不是按钮", Toast.LENGTH_SHORT).show()
            popupWindow.dismiss()//关闭弹窗
        })
    }

效果


http://www.niftyadmin.cn/n/5361967.html

相关文章

LVS常用的NAT模式和DR模式实战示例

引言&#xff1a;紧接上文&#xff0c;了解LVS&#xff0c;这一篇就够了-CSDN博客&#xff0c;今天我们对LVS常用的两种模式来进行示例配置演示 LVS-NAT模式 1、环境准备 准备 3 台纯净的虚拟机 关闭防火墙和selinux 例&#xff1a; lvs-server 添加两个网卡 NAT模式 …

springboot并mybatis入门启动

pom.xml,需要留意jdk的版本&#xff08;11&#xff09;和springboot版本要匹配&#xff08;2.7.4&#xff09;&#xff0c;然后还要注意mybatis启动l类的版本&#xff08;2.2.2&#xff09; <?xml version"1.0" encoding"UTF-8"?> <project xm…

开发智能化企业培训平台:教育系统源码的创新方法

在传统的企业培训模式中&#xff0c;往往面临着效率低下、内容过时以及难以个性化的问题。为了解决这些挑战&#xff0c;采用智能化技术成为了企业培训领域的热门趋势。通过开发智能化企业培训平台&#xff0c;可以提高培训效果、降低成本&#xff0c;并更好地满足员工多样化的…

2024/2/3 牛客寒假算法基础集训营1

目录 why买外卖 G-why买外卖_2024牛客寒假算法基础集训营1 (nowcoder.com) 要有光 L-要有光_2024牛客寒假算法基础集训营1 (nowcoder.com) why买外卖 G-why买外卖_2024牛客寒假算法基础集训营1 (nowcoder.com) 题目要求&#xff1a;这道题要求计算鸡排最贵为多少 思路&a…

springwebflux高性能服务

场景&#xff1a; 分别使用springwebmvc 使用tomcat &#xff08;tomcat 9&#xff09;和springwebflux 做一个简单的接口 &#xff0c;该接口返回一个随机数 压测环境&#xff1a; 4C 8G ECS 使用tomcat 压测结果 Max 抖动的厉害 保持压测的参数不变 使用webflux 压测结果 …

Day25 Golang (回溯) 216.组合总和III 17.电话号码的字母组合

//02 216.组合总和III package mainimport "fmt"func combinationSum3(k int, n int) [][]int {// 全局变量path : []int{}res : [][]int{}// 回溯函数var backtracking func(targetSum, sum, k, startindex int)backtracking func(targetSum, sum, k, startindex i…

使用JDBC连接mysql

JDBC:Java DataBase Connectivity,Java数据库连接。 使用Java语言操作关系型数据库的一套API。 原理&#xff1a;官方&#xff08;sun公司&#xff09;定义出一套操作所有关系型数据库的规则&#xff0c;即接口&#xff1b;所有的数据库厂商去实现这套接口&#xff0c;提供数据…

Linux 系统 ubuntu22.04 发行版本 固定 USB 设备端口号

前言&#xff1a; 项目中为了解决 usb 设备屏幕上电顺序导致屏幕偏移、触屏出现偏移等问题。 一、方法1&#xff1a;使用设备 ID 号 步骤&#xff1a; 查看 USB 设备的供应商ID和产品ID Bus 001 Device 003: ID 090c:1000 Silicon Motion, Inc. - Taiwan (formerly Feiya Te…