Android,适配器中的 RelativeLayout 导致错误

Android, RelativeLayout in Adapter is causing ERROR

我正在尝试制作一个聊天气泡,我已经成功地只用了一个文本,但是当我想添加另一个文本以将时间放入气泡中时,我不得不添加一个 RelativeLayout把它放在实际的线性布局上,但是这次我得到一个错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:transitionGroup="true">


    <RelativeLayout
        android:id="@+id/my_lay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:text="test"
            android:background="@drawable/sender_message_9"/>
        <TextView
            android:id="@+id/messageTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="12:12 PM"
            android:layout_alignBottom="@id/message"
            android:layout_alignRight="@id/message"
            />
    </RelativeLayout>

</LinearLayout>

适配器:

public class ChatAdapter extends ArrayAdapter<MessageProvider> {
    private List<Message> list = new ArrayList<>();
    private TextView TXT;
    private RelativeLayout rLayout;
    Context context;

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.source_message, parent, false);
        }

        ...

        TXT.setText(Message);

        //Set Linear Changes
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(!POSITION){ // Moves the layout to the right
            params.gravity = Gravity.RIGHT;
        }else { // Moves it to the Left
            params.gravity = Gravity.LEFT;
        }
        TXT.setLayoutParams(params);
        return convertView;


    }
}

错误:

05-05 00:10:44.563    8962-8962/com.example.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

如有任何答复,我将不胜感激。

更改以下行

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

因为您已将文本视图包含在 RelativeLayout 父级中。

错误是这一行

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

您正在为相对布局使用线性布局参数。

尝试使用

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);