我的代码这是一个适配器,我希望在列表视图中左右动态对齐我的布局......空指针异常将在relativeParams.leftMargin=80;
爪哇:
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>
请有人帮我把左边距放在我看来......
答案 0 :(得分:1)
我只是将视图输入到相对布局并设置其重力...用于设置参数,意味着边缘我在主相对布局中采用了另一个虚拟相对布局并调整它。
您可以观看以下代码和相对布局
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>
答案 1 :(得分:0)
尝试使用AbsListView.LayoutParams而不是使用RelativeLayout.LayoutParams,因为您的视图位于ListView中。