我目前在使用LinearLayout时遇到了一些问题。我有ListView的这个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/threadBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingStart="10sp"
android:background="@drawable/bubble_green"
android:text="@string/hello_world" />
<TextView
android:id="@+id/threadDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_marginBottom="5sp"
android:text="@string/date"
android:textColor="#808080"
android:visibility="invisible"
android:textSize="12sp" />
</LinearLayout>
它将显示如下内容:
在运行时我有这个适配器将列表视图转换为对话:
public class ThreadAdapter extends ArrayAdapter<Message> {
private Context ctx;
public ArrayList<Message> messageListArray;
public ThreadAdapter(Context context, int textViewResourceId,
ArrayList<Message> messageListArray) {
super(context, textViewResourceId);
this.messageListArray = messageListArray;
this.ctx = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder; // holds the two TextView for better scrolling
View convertView1 = convertView;
LayoutInflater vi = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView1 == null) {
holder = new Holder();
convertView1 = vi.inflate(R.layout.list_view_item_right, parent,
false);
holder.messageContent = (TextView) convertView1
.findViewById(R.id.threadBody);
holder.date = (TextView) convertView1.findViewById(R.id.threadDate);
convertView1.setTag(holder);
} else {
holder = (Holder) convertView1.getTag();
}
Message message = getItem(position);
holder.wrapper = (LinearLayout) convertView1
.findViewById(R.id.parent_wrapper);
try {
if (message.messageNumber.equals("Me")) {
holder.messageContent
.setBackgroundResource(R.drawable.chat_sent);
holder.messageContent.setTextColor(Color.WHITE);
holder.wrapper.setGravity(Gravity.END);
} else {
holder.messageContent
.setBackgroundResource(R.drawable.chat_received);
holder.messageContent.setTextColor(Color.BLACK);
holder.wrapper.setGravity(Gravity.START);
}
} catch (Exception e) {
e.printStackTrace();
}
holder.messageContent.setText(message.messageContent);
holder.date.setText(message.messageDate);
holder.messageContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.date.getVisibility() == TextView.INVISIBLE) {
holder.date.setVisibility(TextView.VISIBLE);
} else {
holder.date.setVisibility(TextView.INVISIBLE);
}
}
});
return convertView1;
}
/**
* @effects returns the size of the array of adapter
*/
@Override
public int getCount() {
return messageListArray.size();
}
/**
* @effects returns the item at specific position
*/
@Override
public Message getItem(int position) {
return messageListArray.get(position);
}
/**
* @effects set the given list as the default list of the adapter
* @param messageList
*/
public void setArrayList(ArrayList<Message> messageList) {
this.messageListArray = messageList;
notifyDataSetChanged();
}
public void addItem(Message msg) {
this.messageListArray.add(msg);
notifyDataSetChanged();
}
private class Holder {
public TextView messageContent, date;
public LinearLayout wrapper;
}
}
它会像这样结束
是否可以在运行时对齐两个textview,这样我就可以在不改变LinearLayout的情况下发送消息了?
谢谢
答案 0 :(得分:1)
进行以下更改: 布局/ list_view_item_right.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/threadBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingStart="10sp"
android:background="@drawable/bubble_green"
android:text="@string/hello_world" />
</LinearLayout>
布局/ list_view_item_date.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/threadDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_marginBottom="5sp"
android:text="@string/date"
android:textColor="#808080"
android:visibility="invisible"
android:textSize="12sp" />
更新代码:
public class ThreadAdapter extends ArrayAdapter<Message> {
private Context ctx;
public ArrayList<Message> messageListArray;
public ThreadAdapter(Context context, int textViewResourceId,
ArrayList<Message> messageListArray) {
super(context, textViewResourceId);
this.messageListArray = messageListArray;
this.ctx = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder; // holds the two TextView for better scrolling
View convertView1 = convertView;
LayoutInflater vi = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Message message = getItem(position);
if (convertView1 == null) {
holder = new Holder();
convertView1 = vi.inflate(R.layout.list_view_item_right, parent,
false);
LinearLayout wrapper= (LinearLayout) convertView1.findViewById(R.id.parent_wrapper);
View view = LayoutInflater.from(ctx).inflate(R.layout.list_view_item_date,null);
if(message.messageNumber.equals("Me")){
wrapper.addView(view,0);
}else{
wrapper.addView(view,1);
}
holder.messageContent = (TextView) convertView1
.findViewById(R.id.threadBody);
holder.date = (TextView) convertView1.findViewById(R.id.threadDate);
convertView1.setTag(holder);
} else {
holder = (Holder) convertView1.getTag();
}
holder.wrapper = (LinearLayout) convertView1
.findViewById(R.id.parent_wrapper);
try {
if (message.messageNumber.equals("Me")) {
holder.messageContent
.setBackgroundResource(R.drawable.chat_sent);
holder.messageContent.setTextColor(Color.WHITE);
holder.wrapper.setGravity(Gravity.END);
} else {
holder.messageContent
.setBackgroundResource(R.drawable.chat_received);
holder.messageContent.setTextColor(Color.BLACK);
holder.wrapper.setGravity(Gravity.START);
}
} catch (Exception e) {
e.printStackTrace();
}
holder.messageContent.setText(message.messageContent);
holder.date.setText(message.messageDate);
holder.messageContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.date.getVisibility() == TextView.INVISIBLE) {
holder.date.setVisibility(TextView.VISIBLE);
} else {
holder.date.setVisibility(TextView.INVISIBLE);
}
}
});
return convertView1;
}
/**
* @effects returns the size of the array of adapter
*/
@Override
public int getCount() {
return messageListArray.size();
}
/**
* @effects returns the item at specific position
*/
@Override
public Message getItem(int position) {
return messageListArray.get(position);
}
/**
* @effects set the given list as the default list of the adapter
* @param messageList
*/
public void setArrayList(ArrayList<Message> messageList) {
this.messageListArray = messageList;
notifyDataSetChanged();
}
public void addItem(Message msg) {
this.messageListArray.add(msg);
notifyDataSetChanged();
}
private class Holder {
public TextView messageContent, date;
public LinearLayout wrapper;
}
}
希望这有效......