在android中我试图根据消息的来源更改布局。 如果消息来自我,则将布局mymessage.xml显示在右侧,然后在左侧显示message.xml。 我使用if条件,但我不知道如何显示一个布局到右边,第二个到左边,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO
View messageView = null;
// Get a reference to the LayoutInflater. This helps construct the
// view from the layout file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Change the layout based on who the message is from
if (messages.get(position).fromMe()) {
messageView = inflater.inflate(R.layout.mymessage , parent, false);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
} else {
messageView = inflater.inflate(R.layout.message , parent, true);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
}
return messageView;
}
答案 0 :(得分:1)
对于left.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/msgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:background="@drawable/textview"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Sampleleft"
android:textColor="#000" />
</RelativeLayout>
对于right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/msgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:background="@drawable/textview"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Sample"
android:textColor="#000" />
</LinearLayout>
只需替换 //根据消息的来源
更改布局 if (messages.get(position).fromMe()) {
messageView = inflater.inflate(R.layout.left , parent, false);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
} else {
messageView = inflater.inflate(R.layout.right , parent, true);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
}
答案 1 :(得分:1)
尝试mymessage.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/mytimeTextView"
android:alpha="0.5"
android:layout_alignParentRight="true"
android:text="5:00 pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="@+id/mytimeTextView"
android:id="@+id/mymessageTextView"
android:layout_alignParentRight="true"
android:text="mymessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
,类似于message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/timeTextView"
android:alpha="0.5"
android:layout_alignParentLeft="true"
android:text="5:00 pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="@+id/timeTextView"
android:id="@+id/messageTextView"
android:layout_alignParentLeft="true"
android:text="yourmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
答案 2 :(得分:1)
for message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/receive_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/message_box"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="visible">
<TextView
android:id="@+id/tv_receiver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="User 1"
android:textColor="@color/NavDrawerTextColor"
android:textSize="14dp"/>
<TextView
android:id="@+id/tvmessagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Hi"
android:textColor="@color/MessageText"
android:textSize="14dp"/>
<TextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="04:16 pm"
android:textSize="10dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/send_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:background="@drawable/message_sender_box"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="visible">
<TextView
android:id="@+id/tvmessagetext_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="user 2"
android:textColor="@color/White"
android:textSize="14dp"/>
<TextView
android:id="@+id/timestamp_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="Hello"
android:textColor="@color/White"
android:textSize="10dp"/>
</LinearLayout>
</RelativeLayout>
for java class:getview()
convertView = inflater.inflate(R.layout.message,parent, false);
if (messages.get(position).fromMe()) {
// for reference receive_ll is GONE and send_ll layout is VISIBLE
} else {
// for reference receive_ll layout is VISIBLE and send_ll layout GONE
}
答案 3 :(得分:0)
说实话吧。
VISIBLE
GONE
类型
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/sender_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
//sender side
</LinearLayout>
<LinearLayout
android:id="@+id/reciver_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
//reciver side
</LinearLayout></LinearLayout>
如果邮件类型为发件人,则sender_layout
可见reciver_layout
答案 4 :(得分:0)
当你获得myMessage时,时间显示mymessage布局,其他布局将隐藏,如下面的编码...
if (messages.get(position).fromMe()) {
myMessageView = inflater.inflate(R.layout.mymessage , parent, false);
myMessageView.setVisibility(View.VISIBLE);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
messageView.setVisibility(View.GONE);
} else {
messageView = inflater.inflate(R.layout.message , parent, true);
messageView.setVisibility(View.VISIBLE);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
myMessageView.setVisibility(View.GONE);
}