我正在尝试将气球消息对齐到屏幕的右侧/末端,尽管在右侧显示,但在设备中查看时,它位于左侧。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/chat_message_top_bottom_padding"
android:paddingBottom="@dimen/chat_message_top_bottom_padding"
android:paddingStart="@dimen/chat_message_right_left_padding"
android:paddingEnd="@dimen/chat_message_right_left_padding"
android:layout_gravity="end"
android:background="@drawable/balloon_shape_sending"
android:theme="@style/ChatMessagesMessageSending"
android:orientation="vertical">
<ImageView
android:id="@+id/img_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="Message" />
<TextView
android:id="@+id/tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Name" />
</LinearLayout>
在Andriod Studio中:
在我的设备中:
用于在屏幕上显示消息的方式是使用ListView
:
<ListView
android:id="@+id/list_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layout_linear"
android:stackFromBottom="true"
android:divider="@android:color/transparent"
android:transcriptMode="alwaysScroll"
tools:listitem="@layout/item_message"/>
负责填充列表的适配器下方:
public class MessageAdapter extends ArrayAdapter<ChatroomMessage> {
MessageAdapter(@NonNull Context context, int resource, List<ChatroomMessage> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message, parent, false);
}
ImageView imgPhoto = convertView.findViewById(R.id.img_photo);
TextView tvAuthor = convertView.findViewById(R.id.tv_author);
TextView tvMessage = convertView.findViewById(R.id.tv_message);
ChatroomMessage message = getItem(position);
if (message.getPhotoUrl() != null) {
tvMessage.setVisibility(View.GONE);
imgPhoto.setVisibility(View.VISIBLE);
Glide.with(imgPhoto.getContext()).load(message.getPhotoUrl()).into(imgPhoto);
} else {
tvMessage.setVisibility(View.VISIBLE);
imgPhoto.setVisibility(View.GONE);
tvMessage.setText(message.getText());
}
tvAuthor.setText(message.getName());
return convertView;
}
}
答案 0 :(得分:0)
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="@dimen/padding_small"
android:paddingEnd="@dimen/padding_avg"
android:paddingStart="@dimen/padding_avg"
android:paddingTop="@dimen/padding_small">
<TextView
android:id="@+id/text_view_chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_huge"
android:layout_toStartOf="@+id/text_view_user_alphabet"
android:background="@drawable/chat_rounded_rect_bg"
android:padding="@dimen/padding_small"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/grey_100"
tools:text="Noice man !" />
<TextView
android:id="@+id/text_view_user_alphabet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginStart="@dimen/margin_avg"
android:background="@drawable/circle_accent"
android:gravity="center"
android:maxEms="1"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/grey_100"
android:textStyle="bold"
tools:text="a" />
</RelativeLayout>
答案 1 :(得分:0)
我不知道为什么答案的例子似乎有效,但这是我的解决方案:
<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:layout_margin="@dimen/margin"
android:orientation="vertical">
<RelativeLayout
android:layout_gravity="end"
android:background="@drawable/balloon_shape_sending"
android:theme="@style/ChatMessagesMessageSending"
android:layout_width="wrap_content"
android:padding="@dimen/padding"
android:layout_height="wrap_content">
...
</RelativeLayout>
</LinearLayout>
答案 2 :(得分:0)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp">
<LinearLayout
android:id="@+id/chat_left_msg_layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
tools:ignore="RtlHardcoded"
android:visibility="gone">
<TextView
android:id="@+id/chat_left_msg_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textSize="16sp"
android:textColor="@color/colorWhite"
android:background="@drawable/recevied_msg_bg"
android:visibility="gone"/>
</LinearLayout>
<LinearLayout
android:id="@+id/chat_right_msg_layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<TextView
android:id="@+id/chat_right_msg_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/colorWhite"
android:layout_margin="10dp"
android:textSize="16sp"
android:background="@drawable/send_msg_bg"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>