好的,尝试这样做
这只是一个示例,您必须在其中添加您的流程
主布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView_Chat"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:background="#123456"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fromMeBt"
android:text="From me" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fromOtherBt"
android:text="From other" />
</LinearLayout>
</LinearLayout>
添加列表行xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/txt_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
创建一个名为 ChatObject 的新类
public class ChatObject {
private String mssg;
private int from;
public ChatObject(String mssg, int from) {
super();
this.mssg = mssg;
this.from = from;
}
public String getMssg() {
return mssg;
}
public void setMssg(String mssg) {
this.mssg = mssg;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
}
现在,像这样对您的 MainActivity 进行一些更改
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ArrayList<ChatObject> items = new ArrayList<ChatObject>();
ListView listView_chat;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new CustomAdapter(items);
listView_chat = (ListView) findViewById(R.id.listView_Chat);
listView_chat.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void fromMeBt(View v) {
ChatObject object = new ChatObject("from me :)", 0);
items.add(object);
adapter.notifyDataSetChanged();
listView_chat.setSelection(items.size());
}
public void fromOtherBt(View v) {
ChatObject object = new ChatObject("from other :)", 1);
items.add(object);
adapter.notifyDataSetChanged();
listView_chat.setSelection(items.size());
}
class CustomAdapter extends BaseAdapter {
ArrayList<ChatObject> items;
public CustomAdapter(ArrayList<ChatObject> items) {
// TODO Auto-generated constructor stub
this.items = items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_chat, null);
TextView txt_chat = (TextView) convertView.findViewById(R.id.txt_chat);
ChatObject object = items.get(position);
txt_chat.setText(object.getMssg());
if (object.getFrom() == 0) {
convertView.setBackgroundColor(Color.BLUE);
}else {
convertView.setBackgroundColor(Color.CYAN);
}
return convertView;
}
}
}
就是这样,如果您想更改 xml 布局中的填充或将其添加到您的 dimens.xml 文件
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
现在开心?我希望这对你有帮助