Android06-點九圖的使用,聊天窗口的搭建

Nine_Patch圖片:一種被特殊處理過的圖片,能夠制定哪些區(qū)域可以被拉伸,哪些區(qū)域不可以

  • 在Android Studio中使用.9圖很簡單:直接將圖片名稱以”.9.png”結(jié)束他嚷。
    使用.9圖必須注意一點:文件的后綴名必須是.9.png,不能是.png或者是.9.png.png芭毙,這樣的命名都會導(dǎo)致編譯失敗筋蓖。
  • 命名完成后雙擊圖片即可進(jìn)行編輯
點九圖02.gif

第一步:創(chuàng)建項目導(dǎo)入圖片

第二步:創(chuàng)建界面布局文件

注:這里因為用到RecyclerView所以要先天際啊RecyclerView相關(guān)庫的依賴。

<?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>

第三步:創(chuàng)建信息窗口布局文件

<?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>

第四步:創(chuàng)建信息實體類

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) {
            //如果是發(fā)出的消息,則顯示在右邊的消息布局苛聘,將左邊的消息布局隱藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}

第六步:加載數(shù)據(jù)

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);

        //初始化消息數(shù)據(jù)
        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);
                    //當(dāng)有新消息時涂炎,刷新顯示
                    adapter.notifyItemInserted(msgList.size()-1);
                    //定位到最后一行
                    msgRecyclerView.scrollToPosition(msgList.size()-1);
                    //清空輸入框的內(nèi)容
                    inputText.setText("");
                }
            }
        });

    }


    private void initMsgs() {
        Msg msg1 = new Msg("很高興認(rèn)識你", 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市设哗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌两蟀,老刑警劉巖网梢,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赂毯,居然都是意外死亡战虏,警方通過查閱死者的電腦和手機(jī)拣宰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來烦感,“玉大人巡社,你說我怎么就攤上這事∈秩ぃ” “怎么了晌该?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绿渣。 經(jīng)常有香客問我朝群,道長,這世上最難降的妖魔是什么中符? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任姜胖,我火速辦了婚禮,結(jié)果婚禮上淀散,老公的妹妹穿的比我還像新娘右莱。我一直安慰自己,他們只是感情好档插,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布隧出。 她就那樣靜靜地躺著,像睡著了一般阀捅。 火紅的嫁衣襯著肌膚如雪胀瞪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天饲鄙,我揣著相機(jī)與錄音凄诞,去河邊找鬼。 笑死忍级,一個胖子當(dāng)著我的面吹牛帆谍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播轴咱,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼汛蝙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了朴肺?” 一聲冷哼從身側(cè)響起窖剑,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎戈稿,沒想到半個月后西土,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡鞍盗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年需了,在試婚紗的時候發(fā)現(xiàn)自己被綠了跳昼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡肋乍,死狀恐怖鹅颊,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情墓造,我是刑警寧澤堪伍,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站滔岳,受9級特大地震影響杠娱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谱煤,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一摊求、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧刘离,春花似錦室叉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至恼除,卻和暖如春踪旷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背豁辉。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工令野, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人徽级。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓气破,卻偏偏與公主長得像,于是被迫代替她去往敵國和親餐抢。 傳聞我的和親對象是個殘疾皇子现使,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容