gpt4 book ai didi

android - 设置 View 的左边距... NullPointerException 来了

转载 作者:行者123 更新时间:2023-11-29 01:36:15 25 4
gpt4 key购买 nike

我的代码这是一个适配器,我想在 ListView 中将我的布局动态地左右对齐...并且空指针异常出现在 relativeParams.leftMargin=80;

Java:

public View getView(int position, View view, ViewGroup parent)
{
ViewHolder viewHolder;
ChatItem chatItem = getItem(position);
viewAligned = view;
if(view == null)
{
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.cell_messages_layout, null);
viewHolder = new ViewHolder();
viewHolder.message = (TextView) view.findViewById(R.id.fromZoneMessage1);
viewHolder.rl = (RelativeLayout) view.findViewById(R.id.messageLayout);
view.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) view.getTag();
}

viewHolder.message.setText(chatItem.getSenderMessage());
if (chatItem.getAllignment() == 0)
{
// viewHolder.message.setGravity(Gravity.LEFT);
viewHolder.message.setBackgroundResource(R.drawable.sent_message);
// view.setBackgroundResource(R.drawable.sent_message);
}
else {
// viewHolder.rl.setBackgroundResource(R.drawable.recieved_message);
// view.holder.rl.set
// viewHolder.message.setGravity(Gravity.RIGHT);
viewHolder.message.setBackgroundResource(R.drawable.recieved_message);

RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams) viewHolder.rl.getLayoutParams();
relativeParams.leftMargin=80;
viewHolder.rl.setLayoutParams(relativeParams);
// LayoutParams lp = (LayoutParams) view.getLayoutParams();
}
return view;
}

我的布局是......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/messageLayout"
>


<TextView
android:id="@+id/fromZoneMessage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:padding="5dp"
android:text="Hey Jack"
/>

</RelativeLayout>

请有人帮我为我的 View 留出左边距....

最佳答案

我只是将 View 类型转换为相对布局并设置其重力...为了设置参数,意味着边距我在主相对布局中采用了另一个虚拟相对布局并对其进行了调整。

你可以看下面的代码和相对布局

public View getView(int position, View view, ViewGroup parent)
{
ViewHolder viewHolder;
ChatItem chatItem = getItem(position);
viewAligned = view;
if(view == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.cell_messages_layout, null);
viewHolder = new ViewHolder();
viewHolder.message = (TextView) view.findViewById(R.id.fromZoneMessage1);
viewHolder.rl = (RelativeLayout) view.findViewById(R.id.messageLayout);
view.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) view.getTag();
}

viewHolder.message.setText(chatItem.getSenderMessage());
if (chatItem.getAllignment() == 0)
{

viewHolder.message.setGravity(Gravity.LEFT);
viewHolder.message.setBackgroundResource(R.drawable.recieved_message);
((RelativeLayout)view).setGravity(Gravity.RIGHT);
android.widget.RelativeLayout.LayoutParams params = new LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 50;
viewHolder.rl.setLayoutParams(params);

}
else {
viewHolder.message.setGravity(Gravity.LEFT);
viewHolder.message.setBackgroundResource(R.drawable.sent_message);
((RelativeLayout)view).setGravity(Gravity.LEFT);
android.widget.RelativeLayout.LayoutParams params = new LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.rightMargin = 50;
viewHolder.rl.setLayoutParams(params);
}

return view;
}

布局

<?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"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/messageLayout"
>


<TextView
android:id="@+id/fromZoneMessage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:padding="5dp"
android:text="Hey Jack"/>

</RelativeLayout>
</RelativeLayout>

关于android - 设置 View 的左边距... NullPointerException 来了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27796253/

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