通过蓝牙实现手机与手机聊天,手机与单片机通讯(一.布局篇)

一.布局篇

布局的大致就是这样,主要是一个聊天界面,一个蓝牙选择界,其中有个菜单,还有个聊天左右显示。

①:首先我们布局方面先弄一个聊天界面作为主界面,一个RecyclView和一个EditText,Button。效果如下:

代码如下:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/input_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/send1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"/>
    </LinearLayout>
</LinearLayout>

②:我们在标题栏上面添加一个菜单,里面的选项有:蓝牙连接,(这里可以添加任何你想加的按钮,作者本人添加的是蓝牙遥控),效果如下:


代码如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/bluetooth"
        android:title="蓝牙连接"/>
    <item
        android:id="@+id/corcar"
        android:title="小车遥控"/>
</menu>

③:接下来我们在菜单的“蓝牙连接按钮“上实现点击后加载另一个布局,这也就是我们选择要连接哪个蓝牙的布局,这个布局就只是单纯的一个ListView,效果如下:

代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ListView
        android:id="@+id/list_item1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

④:在最后我们一开始就说弄一个聊天界面,然后聊天界面肯定有左右消息的布局,具体操作在接下来代码中适配器会用到,效果如下:

代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start">
        <TextView
            android:text="asd"
            android:id="@+id/left_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        >
        <TextView
            android:text="asd"
            android:id="@+id/right_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            />
    </LinearLayout>
</LinearLayout>

布局方面就先如上所示。代码方面请看第二篇。

https://mp.csdn.net/postedit/81283749

猜你喜欢

转载自blog.csdn.net/qq_42246939/article/details/81281939