Android Studio开发学习(六、蓝牙模块xml)

引言

        在上面的学习中,我们在侧滑界面添加了一个按钮,名称为蓝牙( 上篇侧滑界面详情请看Android Studio开发学习(三、侧滑界面))。我们新建一个BlueTooth包,里面包含蓝牙相关的功能函数 BlueToothActivity.java,对应布局在layout包下activity_bluetooth.xml。

下篇博客,我们介绍对应方法实现,完整代码在下篇博客放出。

布局介绍

我们先来介绍一下射击的新布局 ConstraintLayout、Guideline。

        ConstraintLayout 允许开发者通过定义视图之间的约束关系来创建复杂的用户界面布局。与传统的布局方式(如LinearLayout、RelativeLayout等)相比,ConstraintLayout 能够显著减少布局层级的嵌套,从而提高应用的性能和渲染效率。(高版本默认布局)

        ConstraintLayout适用于需要创建复杂布局但又希望减少布局层级和提高性能的场景。它特别适用于响应式设计和动态布局调整的应用,如适配不同屏幕尺寸和方向、实现复杂的交互效果等。

        Guideline 是 Android 开发中 ConstraintLayout(约束布局)中的一个重要概念,它作为一种特殊的控件,用于在布局中定义参考线,帮助开发者更精确地定位和排列其他视图(View)。Guideline 本身在界面上是不可见的,仅作为布局中的辅助工具使用。   

对称布局:当需要实现两个或多个视图在屏幕上对称显示时,可以使用Guideline来定义对称轴。

动态布局:在需要根据屏幕尺寸或方向变化自动调整布局的场景中,Guideline的百分比定位功能非常有用。

复杂布局:对于包含多个视图且布局关系复杂的界面,使用Guideline可以帮助开发者更清晰地组织和定位视图。

布局预览

完整代码

下篇博客,我们介绍对应方法实现,完整代码在下篇博客放出。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background1"
    tools:context=".BlueTooth.BlueToothActivity">


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="136dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/listView1"
        app:layout_constraintGuide_end="206dp"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/openBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="打开蓝牙"
        app:layout_constraintBottom_toTopOf="@+id/stateBT"
        app:layout_constraintEnd_toStartOf="@id/guideline1"
        app:layout_constraintHorizontal_bias="0.567"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/closeBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="关闭蓝牙"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.473"
        app:layout_constraintStart_toEndOf="@id/guideline1"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/stateBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蓝牙状态"
        app:layout_constraintEnd_toStartOf="@id/guideline1"
        app:layout_constraintHorizontal_bias="0.567"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/openBT" />

    <Button
        android:id="@+id/updateList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更新列表"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.473"
        app:layout_constraintStart_toEndOf="@id/guideline1"
        app:layout_constraintTop_toBottomOf="@id/closeBT" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="307dp"
        android:layout_height="46dp"
        android:layout_marginTop="92dp"
        android:background="#2196F3"
        android:gravity="center"
        android:text="蓝牙列表"
        android:textSize="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/stateBT" />
    <ListView
        android:id="@+id/listView1"
        android:layout_width="332dp"
        android:layout_height="350dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1"
        app:layout_constraintVertical_bias="0.316" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中连接HC-05蓝牙模块,需要使用Android的蓝牙API和串口通信协议,具体步骤如下: 1. 在AndroidManifest.xml文件中添加蓝牙权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 2. 在应用程序中使用BluetoothAdapter类来获取本地蓝牙适配器: ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); ``` 3. 使用BluetoothAdapter对象来搜索HC-05蓝牙模块: ```java bluetoothAdapter.startDiscovery(); ``` 4. 连接HC-05蓝牙模块。首先需要获取HC-05蓝牙模块的MAC地址,然后使用BluetoothDevice类来连接蓝牙模块: ```java String mac = "00:11:22:33:44:55"; // HC-05蓝牙模块的MAC地址 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(mac); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid); socket.connect(); ``` 其中,uuid是HC-05蓝牙模块服务的UUID,可以在HC-05模块的说明书中找到。 5. 使用IO流进行数据的传输和通信。可以使用InputStream和OutputStream来进行数据的读写操作: ```java InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); byte[] buffer = new byte[1024]; int bytes; bytes = inputStream.read(buffer); outputStream.write(buffer, 0, bytes); ``` 6. 最后,在应用程序退出时需要关闭蓝牙连接: ```java socket.close(); ``` 以上是Android Studio连接HC-05蓝牙模块的基本步骤,具体的实现过程需要根据应用程序的需求和实际情况进行相应的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值