gpt4 book ai didi

java - android聊天缩略图位置

转载 作者:行者123 更新时间:2023-11-30 11:09:08 25 4
gpt4 key购买 nike

我有一个带有气泡和用户缩略图的对话页面,缩略图位置有问题:当我发送文本时,缩略图必须显示在右侧,而当我的 friend 发送文本时,他的缩略图必须显示在左侧

我的 XML 项目是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >

<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0.5dp" >

<ImageView
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/user_four"
android:padding="0dp" />

<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:textColor="#2e2e2e"
android:paddingLeft="10dp" />
</LinearLayout>

</RelativeLayout>

我的 Java 代码是:

public class ChatArrayAdapter extends ArrayAdapter<Comment> {

private TextView countryName;
private List<Comment> countries = new ArrayList<Comment>();
private LinearLayout wrapper;


@Override
public void add(Comment object) {
countries.add(object);
super.add(object);
}

public ChatArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

public int getCount() {
return this.countries.size();
}

public Comment getItem(int index) {
return this.countries.get(index);
}

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_chat, parent, false);
}

wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

Comment coment = getItem(position);

countryName = (TextView) row.findViewById(R.id.comment);

countryName.setText(coment.comment);

countryName.setBackgroundResource(coment.left ? R.drawable.other_bubble : R.drawable.own_bubble);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);


return row;
}

public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

}

最佳答案

如果您在 getView() 方法中知道文本是您或您 friend 的,您可以根据条件将线性布局的重力属性更改为向左或向右。

关于java - android聊天缩略图位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28260794/

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