• Android开发基础——UI实践


    这里编写一个聊天界面。

    制作9-Patch图片

    9-Patch图片是一种被特殊处理过的png图片,能够指定哪些区域可以被拉伸,哪些区域不可以。

     比如上面的图片,如果直接设置为背景图:

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="50dp"
    5. android:background="@drawable/message_left_original">
    6. LinearLayout>

    程序运行结果为: 

     可以看到,由于图片宽度不足以填满整个屏幕的宽度,因此整张图片被拉伸了,这种显示效果肯定是不行的,此时需要用到9-Patch图片。

    在该图片右击,选择Create 9-Patch file,然后保存,保存后双击图片会出现如下编辑界面:

     此时可以在图片的4个边框绘制一个个的小黑点,在上边框和左边框绘制的部分表示当图片需要拉伸时就拉伸黑点标记的区域,在下边框和右边框绘制的部分表示内容允许被放置的区域,使用鼠标在图片的边缘拖动就可以进行绘制了,按住shift键可进行擦除。绘制完成后为:

     重新运行程序的结果为:

     编写聊天界面

    首先在app/build.gradle中添加依赖:

    1. dependencies {
    2. implementation 'androidx.core:core-ktx:1.3.2'
    3. implementation 'androidx.appcompat:appcompat:1.2.0'
    4. implementation 'com.google.android.material:material:1.3.0'
    5. implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    6. implementation 'androidx.recyclerview:recyclerview:1.2.0'
    7. testImplementation 'junit:junit:4.+'
    8. androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    9. androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    10. }

    然后修改activity_main.xml:

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:orientation="vertical"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:background="#d8e0e8">
    8. <androidx.recyclerview.widget.RecyclerView
    9. android:id="@+id/recyclerView"
    10. android:layout_width="match_parent"
    11. android:layout_height="0dp"
    12. android:layout_weight="1"/>
    13. <LinearLayout
    14. android:layout_width="match_parent"
    15. android:layout_height="wrap_content">
    16. <EditText
    17. android:id="@+id/inputText"
    18. android:layout_width="0dp"
    19. android:layout_height="wrap_content"
    20. android:layout_weight="1"
    21. android:hint="Type something here"
    22. android:maxLines="2"
    23. tools:ignore="Suspicious0dp" />
    24. <Button
    25. android:id="@+id/send"
    26. android:layout_width="wrap_content"
    27. android:layout_height="wrap_content"
    28. android:text="Send"/>
    29. LinearLayout>
    30. LinearLayout>

    上面的代码中,使用RecyclerView用于显示聊天内容,使用EditText输入消息,Button用于发送消息。

    然后定义消息实体类,新建Msg:

    1. class Msg(val content:String, val type:Int) {
    2. companion object {
    3. const val TYPE_RECEIVED = 0
    4. const val TYPE_SENT = 1
    5. }
    6. }

    上面Msg类中只有两个字段,content表示消息内容,type表示消息类型,其中消息类型有两个值:TYPE_RECEIVED表示收到的消息,TYPE_SENT表示发送的消息。

    然后编写RecyclerView子项布局,新建msg_left_item.xml:

    1. "1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="wrap_content"
    5. android:padding="10dp">
    6. <LinearLayout
    7. android:layout_width="wrap_content"
    8. android:layout_height="wrap_content"
    9. android:layout_gravity="left"
    10. android:background="@drawable/message_left">
    11. <TextView
    12. android:id="@+id/leftMsg"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:layout_gravity="center"
    16. android:layout_margin="10dp"
    17. android:textColor="#fff"/>
    18. LinearLayout>
    19. FrameLayout>

    收到的消息是左对齐的。类似的,还要编写一个发送消息的子项布局,新建msg_right_item.xml:

    1. "1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="wrap_content"
    5. android:padding="10dp">
    6. <LinearLayout
    7. android:layout_width="wrap_content"
    8. android:layout_height="wrap_content"
    9. android:layout_gravity="right"
    10. android:background="@drawable/message_right">
    11. <TextView
    12. android:id="@+id/rightMsg"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:layout_gravity="center"
    16. android:layout_margin="10dp"
    17. android:textColor="#000"/>
    18. LinearLayout>
    19. FrameLayout>

    发送的消息是右对齐的。然后创建RecyclerView的适配器类,新建类MsgAdapter:

    1. class MsgAdapter(val msgList: List):
    2. RecyclerView.Adapter(){
    3. inner class LeftViewHolder(view:View):RecyclerView.ViewHolder(view) {
    4. val leftMsg: TextView = view.findViewById(R.id.leftMsg)
    5. }
    6. inner class RightViewHolder(view:View):RecyclerView.ViewHolder(view) {
    7. val rightMsg: TextView = view.findViewById(R.id.rightMsg)
    8. }
    9. override fun getItemViewType(position: Int): Int {
    10. val msg = msgList[position]
    11. return msg.type
    12. }
    13. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = if (viewType == Msg.TYPE_RECEIVED) {
    14. val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_left_item, parent, false)
    15. LeftViewHolder(view)
    16. } else {
    17. val view = LayoutInflater.from(parent.context).inflate(R.layout.msg_right_item, parent, false)
    18. RightViewHolder(view)
    19. }
    20. override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    21. val msg = msgList[position]
    22. when (holder) {
    23. is LeftViewHolder -> holder.leftMsg.text = msg.content
    24. is RightViewHolder -> holder.rightMsg.text = msg.content
    25. else -> throw IllegalArgumentException()
    26. }
    27. }
    28. override fun getItemCount() = msgList.size
    29. }

    上面代码中根据不同的viewType创建不同的界面。首先定义了LeftViewHolder和RightViewHolder这两个ViewHolder,分别用于缓存msg_left_item.xml和msg_right_item.xml布局中的控件。然后重写getItemViewType方法并返回当前position对应的消息类型。

    然后在onCreateViewHolder方法总根据不同的viewType来加载不同的布局并创建不同的ViewHolder,然后在onBindViewHolder方法中判断ViewHolder的类型,如果是LeftViewHolder,就将内容显示到左边的消息布局,如果是RightViewHolder,就将内容显示到右边的消息布局。

    然后修改Activity,为RecyclerView初始化数据,并加入按钮点击事件:

    1. class MainActivity : AppCompatActivity(), View.OnClickListener {
    2. private val msgList = ArrayList()
    3. private var adapter:MsgAdapter ?= null
    4. override fun onCreate(savedInstanceState: Bundle?) {
    5. super.onCreate(savedInstanceState)
    6. setContentView(R.layout.activity_main)
    7. initMsg()
    8. val layoutManager = LinearLayoutManager(this)
    9. recyclerView.layoutManager = layoutManager
    10. adapter = MsgAdapter(msgList)
    11. recyclerView.adapter = adapter
    12. send.setOnClickListener(this)
    13. }
    14. override fun onClick(v: View?) {
    15. when (v) {
    16. send -> {
    17. val content = inputText.text.toString()
    18. if (content.isNotEmpty()) {
    19. val msg = Msg(content, Msg.TYPE_SENT)
    20. msgList.add(msg)
    21. adapter?.notifyItemInserted(msgList.size - 1)
    22. recyclerView.scrollToPosition(msgList.size - 1)
    23. inputText.setText("")
    24. }
    25. }
    26. }
    27. }
    28. private fun initMsg() {
    29. val msg1 = Msg("Hello,guy:", Msg.TYPE_RECEIVED)
    30. msgList.add(msg1)
    31. val msg2 = Msg("Hello,who is that?", Msg.TYPE_SENT)
    32. msgList.add(msg2)
    33. val msg3 = Msg("This is Tom. Nice to meet you.", Msg.TYPE_RECEIVED)
    34. msgList.add(msg3)
    35. }
    36. }

    上面代码中,现在initMsg方法中初始化了几条数据用于在RecyclerView中显示,然后按照标准方式构建RecyclerView,并为其指定了LayoutManager和适配器。

    然后再发送按钮的点击事件中获取了EditText中的内容,如果内容不为空,就创建一个新的Msg对象添加到msgList列表中,然后调用适配器的notifyItemInserted方法用于通知列表有新的数据插入,这样新增的消息才能够在RecyclerView中显示出来。或者可以使用notifyDataSetChanged方法将所有可见元素刷新,不过该方法效率较差。然后调用scrollToPosition方法将显示的数据定位到最后一行,以保证一定可以看得到最后发出的一条消息。最后清空EditText的内容。

    程序运行后的结果为:

  • 相关阅读:
    sql处理重复的列,更好理清分组和分区
    减少软件故障、防范黑客攻击,软件质量安全问题不容忽视
    利用人工智能和大数据技术,优化IT运维流程和策略
    从输入URL到展示出页面
    远程服务器配置 Anaconda 并安装 PyTorch 详细教程
    2023陇剑杯
    【算法】剑指offer-杨氏数组&&旋转数组
    驱动模块编译遇到一些问题解决
    STM32内部flash详解(1)
    外包干了3个多月,技术退步明显。。。。
  • 原文地址:https://blog.csdn.net/SAKURASANN/article/details/126922921