gpt4 book ai didi

android:如何更改 ListView 中 row_item 的 layout_gravity 属性

转载 作者:太空宇宙 更新时间:2023-11-03 10:19:56 26 4
gpt4 key购买 nike

我正在尝试实现一个可以出现在左侧或右侧的聊天气泡。我阅读了很多已在此处发布的答案,但我没有设法让其中任何一个为我工作,也许我遗漏了什么。

如果我手动将 row_item xml 中 linearLayout 的 layout_gravity 从左更改为右,然后查看图形布局,我可以看到它按我想要的方式工作。问题是我无法控制此属性。据我了解,问题与存在的 LayoutParams 属于 listview.LayoutParams 这一事实有关,如果我尝试将 LayoutParams 获取到 listview.LayoutParams 对象,它会起作用。但这不是我想要实现的目标。

代码如下:

Activity xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MergeRootFrame" >

<ListView
android:id="@+id/listViewMessages"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.98" >

</ListView>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.02"
android:background="#D3D3D3"
android:orientation="horizontal" >

<EditText
android:id="@+id/editTextMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/imageButtonSend"
android:ems="10"
android:hint="@string/textFieldMessage"
android:textSize="14sp"
android:visibility="visible" >

<requestFocus />
</EditText>

<ImageButton
android:id="@+id/imageButtonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/imageButtonSendNow_contentDescription"
android:src="@drawable/ic_action_send_now"
android:onClick="onClick" />

</RelativeLayout>

</LinearLayout>

row_item xml:

<?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:id="@+id/singleMessageContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal"
android:padding="6dp" >

<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="0.85"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:gravity="center_vertical"
android:text="Message"
android:textSize="16sp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/TextViewTime"
android:layout_width="wrap_content"
android:layout_height="14dp"
android:layout_weight="0.1"
android:layout_gravity="bottom"
android:layout_marginLeft="22dp"
android:gravity="center_vertical"
android:text="Time"
android:textSize="12sp"
tools:ignore="HardcodedText" />

<ImageView
android:id="@+id/imageViewSuccess"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_weight="0.05"
android:layout_gravity="bottom"
android:contentDescription="successIcon"
android:src="@drawable/ic_action_accept"
tools:ignore="HardcodedText" />

</LinearLayout>

适配器代码:

public class ConversationAdapter extends ArrayAdapter<Message> {

private final Context context;

static class ViewHolder {
public TextView message;
public TextView time;
public ImageView success;
public LinearLayout singleMessageContainer;

}

public ConversationAdapter(Context context, ArrayList<Message> values) {
super(context, R.layout.conversation_list_item, values);
this.context = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.conversation_list_item, parent,false);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.message = (TextView) rowView.findViewById(R.id.textViewMessage);
viewHolder.time = (TextView) rowView.findViewById(R.id.TextViewTime);
viewHolder.success = (ImageView) rowView.findViewById(R.id.imageViewSuccess);
viewHolder.singleMessageContainer = (LinearLayout) rowView.findViewById(R.id.singleMessageContainer);

rowView.setTag(viewHolder);
}

// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();

holder.message.setText(getItem(position).getMessage());
holder.time.setText(getItem(position).getUniversalTimeString());
holder.success.setImageResource(R.drawable.ic_action_accept);

boolean isMine = (getItem(position).getSender() == 0);

holder.singleMessageContainer.setBackgroundResource(
isMine ? R.drawable.bubble_yellow : R.drawable.bubble_green);

// This part doesn't work
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
holder.singleMessageContainer.getLayoutParams();
params.gravity=(isMine ? Gravity.LEFT : Gravity.RIGHT);

holder.singleMessageContainer.setLayoutParams(params);

return rowView;
}

}

最佳答案

解决方案

我继续阅读有关 gravity 和 layout_gravity 的内容,并遇到了帮助我解决此问题的更详细信息。在这里问题的几个答案中,有一个使用 warpper 布局的解决方案,但没有写下需要注意的重要事项。

首先要知道的是,如果您将线性布局设置为水平布局,您将无法将重力属性用作左或右仅顶部或底部因此外层容器必须是垂直的,而内层容器必须是水平的,以便在一行中一个接一个地显示消息和其他数据。

然后您可以使用外部容器的方法 setGravity(Gravity.LEFT) 或 setGravity(Gravity.RIGHT) 将内部 warpper 正确对齐到左侧或右侧。您还需要注意 fill_parent 和 wrap_content 的属性

这里是更新的代码:row_item xml:

<?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:id="@+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:padding="6dp" >

<LinearLayout
android:id="@+id/warpper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textViewMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/TextViewTime"
android:layout_width="wrap_content"
android:layout_height="14dp"
android:layout_gravity="bottom"
android:text="Time"
android:textSize="12sp"
tools:ignore="HardcodedText" />

<ImageView
android:id="@+id/imageViewSuccess"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:layout_gravity="bottom"
android:contentDescription="successIcon"
android:src="@drawable/ic_action_accept"
tools:ignore="HardcodedText" />

</LinearLayout>

</LinearLayout>

和更新后的适配器代码:

public class ConversationAdapter extends ArrayAdapter<Message> {

private final Context context;

static class ViewHolder {
public TextView message;
public TextView time;
public ImageView success;
public LinearLayout singleMessageContainer;
// Added
public LinearLayout warpper;

}

public ConversationAdapter(Context context, ArrayList<Message> values) {
super(context, R.layout.conversation_list_item, values);
this.context = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.conversation_list_item, parent,false);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.message = (TextView) rowView.findViewById(R.id.textViewMessage);
viewHolder.time = (TextView) rowView.findViewById(R.id.TextViewTime);
viewHolder.success = (ImageView) rowView.findViewById(R.id.imageViewSuccess);
viewHolder.singleMessageContainer = (LinearLayout) rowView.findViewById(R.id.singleMessageContainer);
// Added
viewHolder.warpper = (LinearLayout) rowView.findViewById(R.id.warpper);

rowView.setTag(viewHolder);
}

// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();

holder.message.setText(getItem(position).getMessage());
holder.time.setText(getItem(position).getUniversalTimeString());
holder.success.setImageResource(R.drawable.ic_action_accept);

boolean isMine = (getItem(position).getSender() == 0);

// Changed to warpper
holder.warpper.setBackgroundResource(
isMine ? R.drawable.bubble_yellow : R.drawable.bubble_green);
// Added
holder.singleMessageContainer.setGravity(isMine ? Gravity.LEFT : Gravity.RIGHT);


return rowView;
}

}

它应该可以工作。唯一的问题是,您可能会在 xml 中看到一条关于线性布局之一无用的警告,但没有它就无法工作。

希望对你有所帮助。

关于android:如何更改 ListView 中 row_item 的 layout_gravity 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25351057/

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