3-18。チャットインタフェースを書きます

生産ナインパッチの写真

ナインパッチ画像特別な処理が画像をPNG形式れていた、どの領域がなくてもよく、延伸することができる領域を指定することが可能です。

以下に示すようにactivity_main.xml中のLinearLayoutの背景画像として、これらの上記画像は、コードを変更します。

1 
2
3
4
5
6
7
<?xml version = "1.0"エンコード= "UTF-8"?> 
< のLinearLayout のxmlns:アンドロイド = "http://schemas.android.com/apk/res/android" アンドロイド:layout_width = "match_parent" アンドロイド:layout_height = "wrap_content" アンドロイド:背景 = "@描画可能/ message_left" >




</ のLinearLayout >

次のように業績があります。

Googleは理由により、内部のAndroid Studioに統合draw9patch.batの人気draw9patch。

在AS中,右键需要编辑的图片,选择Create 9-Patch file进行编辑,会自动生成.9格式的图片,打开该格式的图片可进行编辑。

在边框部分绘制黑点表示图片需要拉伸时就拉伸黑点标记的部分。

运行效果如下。

编写聊天界面

添加依赖库

在app/build.gradle中添加RecyclerView的依赖库。

1
compile 'com.android.support:recyclerview-v7:24.2.1'

编写主界面

修改activity_main.xml文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/input_text"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send"
android:text="Send"/>

</LinearLayout>

</LinearLayout>

定义消息的实体类

新建消息的实体类Msg。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class  {
public static final int TYPE_RECEIVED=0;
public static final int TYPE_SENT=1;
private String content;
private int type;

public (String content,int type){
this.content=content;
this.type=type;
}

public String getContent(){
return content;
}

public int getType(){
return type;
}
}

编写RecyclerView的子项

新建msg_item.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_layout"
android:layout_gravity="left"
android:background="@drawable/message_left">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" 大专栏  3-18.编写聊天界面an>
android:id="@+id/left_msg"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_layout"
android:layout_gravity="right"
android:background="@drawable/message_left">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_msg"
android:layout_gravity="center"
android:layout_margin="10dp"/>

</LinearLayout>

</LinearLayout>

创建RecyclerView的适配器

新建MsgAdapter类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{
private List<Msg> mMsgList;

static class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
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;
}


public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new ViewHolder(view);
}


public void onBindViewHolder(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());
}
}


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

为RecyclerView加入初始化数据&为发送按钮注册监听事件

修改MainActivity。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class MainActivity extends AppCompatActivity {

private List<Msg> msgList=new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initMsg();
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() {

public void onClick(View view) {
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);//将ListView定位到最后一行
inputText.setText("");//清空输入框中的内容
}
}
});
}

プライベート ボイド initMsg () {
メッセージのMSG1は=新しいメッセージ( "1"、Msg.TYPE_RECEIVED)。
msgList.add(MSG1)。
メッセージのMSG2 =新しいメッセージ( "2"、Msg.TYPE_SENT)。
msgList.add(MSG2)。
メッセージのメッセージ3 =新しいメッセージ( "2"、Msg.TYPE_RECEIVED)。
msgList.add(メッセージ3)。
}
}

次のように業績があります。

おすすめ

転載: www.cnblogs.com/dajunjun/p/11711067.html