适配器中的Android,RelativeLayout导致ERROR

时间:2015-05-05 04:14:47

标签: android android-listview android-linearlayout relativelayout

我正在尝试制作聊天泡泡,我已经成功地使用了一个文本,但是当我想添加另一个文本以将时间放入泡泡时我必须添加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>

ADAPTER:

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;


    }
}

ERROR:

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

感谢您的回答,谢谢。

2 个答案:

答案 0 :(得分:3)

更改以下行

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);

因为您已将textview包含在RelativeLayout父级内。

答案 1 :(得分:1)

错误就是这一行

 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);