gpt4 book ai didi

java - 在android中显示布局1在左边,布局2在右边

转载 作者:行者123 更新时间:2023-12-01 19:56:56 25 4
gpt4 key购买 nike

在android中,我试图根据消息的来源来更改布局。如果消息来 self ,则在右侧显示布局 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;
}

最佳答案

尝试使用 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>

关于java - 在android中显示布局1在左边,布局2在右边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49318756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com