Android06-點九圖的使用,聊天視窗的搭建
Nine_Patch圖片:一種被特殊處理過的圖片,能夠制定哪些區域可以被拉伸,哪些區域不可以
- 在Android Studio中使用.9圖很簡單:直接將圖片名稱以”.9.png”結束。
使用.9圖必須注意一點:檔案的字尾名必須是.9.png,不能是.png或者是.9.png.png,這樣的命名都會導致編譯失敗。- 命名完成後雙擊圖片即可進行編輯

點九圖02.gif
第一步:建立專案匯入圖片
第二步:建立介面佈局檔案
注:這裡因為用到RecyclerView所以要先天際啊RecyclerView相關庫的依賴。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8e0e8"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/msg_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="0dp"
android:hint="請輸入資訊"
android:maxLines="2"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
</LinearLayout>
第三步:建立資訊視窗布局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/message_left">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">
<TextView
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>
第四步:建立資訊實體類
package com.example.anwser_mac.uibestpractice;
/**
* Created by anwser_mac on 2017/4/1.
*/
public class Msg {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
private String content;
private int type;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public int getType() {
return type;
}
}
第五步:自定義RecyclerView介面卡
package com.example.anwser_mac.uibestpractice;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
/**
* Created by anwser_mac on 2017/4/1.
*/
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.Viewholder> {
private List<Msg> mMsgList;
static class Viewholder extends RecyclerView.ViewHolder {
android.widget.LinearLayout leftLayout;
android.widget.LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public Viewholder(View view) {
super(view);
leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
leftMsg = (TextView) view.findViewById(R.id.left_msg);
rightMsg = (TextView) view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<Msg> msgList) {
mMsgList = msgList;
}
@Override
public MsgAdapter.Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(MsgAdapter.Viewholder holder, int position) {
Msg msg = mMsgList.get(position);
if (msg.getType() == Msg.TYPE_RECEIVED) {
//如果是收到的訊息,則顯示在左邊的訊息佈局,將右邊的訊息佈局隱藏
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
} else if(msg.getType() == Msg.TYPE_SENT) {
//如果是發出的訊息,則顯示在右邊的訊息佈局,將左邊的訊息佈局隱藏
holder.rightLayout.setVisibility(View.VISIBLE);
holder.leftLayout.setVisibility(View.GONE);
holder.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
}
第六步:載入資料
package com.example.anwser_mac.uibestpractice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Msg> msgList = new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
//初始化訊息資料
initMsgs();
inputText = (EditText) findViewById(R.id.input_text);
send = (Button) findViewById(R.id.send);
msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
adapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(adapter);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = inputText.getText().toString();
if (!"".equals(content)) {
Msg msg = new Msg(content, Msg.TYPE_SENT);
msgList.add(msg);
//當有新訊息時,重新整理顯示
adapter.notifyItemInserted(msgList.size()-1);
//定位到最後一行
msgRecyclerView.scrollToPosition(msgList.size()-1);
//清空輸入框的內容
inputText.setText("");
}
}
});
}
private void initMsgs() {
Msg msg1 = new Msg("很高興認識你", Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg("你高興得太早了", Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3 = new Msg("你醜開", Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}

點九圖使用例項.gif
相關文章
- 聊天視窗
- Qt TCP (小型聊天視窗)QTTCP
- vscode原始碼分析【九】視窗裡的主要元素VSCode原始碼
- plsql developer 視窗的使用SQLDeveloper
- 使用Windows API進行GDI視窗繪圖WindowsAPI繪圖
- Mac下給視窗截圖的快捷鍵Mac
- 使用WebRTC技術搭建小型的影片聊天頁面Web
- winform 跨視窗點選ORM
- mysql視窗函式中的滑動視窗MySql函式
- MySQL視窗函式的具體使用TOCSMySql函式
- Flink的滾動視窗、會話視窗、滑動視窗及其應用會話
- 對滑動視窗單調性的一點思考
- .NET 視窗/螢幕截圖
- 視窗程式的框架框架
- 如何修改CAD夢想畫圖繪圖視窗的背景顏色繪圖
- python中視窗主體框架如何搭建?Python框架
- hive視窗函式使用Hive函式
- 怎麼去掉Mac視窗截圖自帶的陰影?Mac
- PyQt5 帶視窗圖示QT
- ICCV2021 | Swin Transformer: 使用移位視窗的分層視覺TransformerORM視覺
- Qt 子視窗 隱藏標題欄的圖示,隱藏在工作列上的圖示QT
- dotnet 如何從 Gtk 3 的視窗到對應的 X11 視窗
- win10切換視窗的快捷鍵是什麼_win10如何使用切換視窗的快捷鍵Win10
- 基於Vue的點對點聊天專案Vue
- Python 指令碼自動視窗截圖Python指令碼
- AutoCAD快速入門(二):圖形視窗
- 【教程】使用gitee搭建免費的圖床Gitee圖床
- 【Vegas原創】解決cmd視窗不夠使用的方法
- 在Mac中如何用⌘鍵拖拽非使用中的視窗?Mac
- 【Minitab使用教程】認識六西格瑪Minitab的視窗
- PostgreSQL>視窗函式的用法SQL函式
- 建立多視窗的Windowsform程式WindowsORM
- fastadmin 彈出視窗的功能AST
- P1502 視窗的星星
- 基本的Dos命令(cmd視窗)
- 躺平吧,平鋪的視窗「GitHub 熱點速覽 v.21.47」Github
- 短視訊平臺搭建,收到通知後彈出相關提示的彈窗
- 去掉vs中的console的黑視窗
- 關於父視窗獲取跨域iframe子視窗中的元素跨域