我试图将气球消息对准屏幕的右/末端,尽管显示在右边,但在设备中查看时却在左边。
<?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;
}
}
最佳答案
我不知道为什么答案示例似乎奏效了,但这是我的解决方案:
<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>
关于java - android:layout_gravity =“end”在实际设备中编译时不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50576139/