RelativeLayout没有响应TextView

时间:2015-08-29 12:32:22

标签: android android-layout

我有 ListView RelativeLayout ,请参阅下文:

MainActivity.java

adapter = new ArrayAdapter<String>(this, R.layout.listviewitemtext, sortedmessage){
        public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
            View row = convertView;
        TextView text1 = (TextView) view.findViewById(id.msgThread);
            String msgBody=msgThread.getItemAtPosition(position).toString();
            char lastChar=msgBody.charAt(msgBody.length()-1);
            if( lastChar=='1'){
                text1.setText(msgBody.substring(0, msgBody.length() - 1));
                text1.setBackgroundResource(drawable.incoming_bubble_telegram);
                text1.setTextColor(Color.BLACK);
                text1.setPadding(60,60,60,60);
            }
            else if( lastChar=='0'){
                text1.setText(msgBody.substring(0, msgBody.length() - 1));
                text1.setBackgroundResource(drawable.outgoing_bubble_telegram_selected);
                text1.setTextColor(Color.WHITE);
                text1.setPadding(60, 60, 60, 60);

            }
        return view;

    };};
    msgThread.setAdapter(adapter);`

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.clyde.listviewtest.MessageThread">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="500dp"
        android:id="@+id/msgList"
        android:divider="@android:color/transparent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:background="#FFFFFF"
        />
</RelativeLayout>

listviewitemtext.xml

<TextView
        android:id="@+id/msgThread"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:maxWidth="1000px"
        android:layout_alignParentRight="true"
 />

我想要实现的是将右侧的textview对齐。 layout_alignParentRight 似乎无效。当我在TextView上使用 RelativeLayout 时,我的应用程序崩溃/没有响应。任何帮助将不胜感激。

注意:请注意,Listview在MainActivity.xml中设置样式,TextView在单独的xml文件(listviewitems.xml)中设置样式。我将此listviewitems.xml应用为我的适配器中的自定义布局。

堆栈跟踪:

>08-29 20:55:39.371    2474-2474/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.clyde.listviewtest, PID: 2474
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at com.example.clyde.listviewtest.MessageThread$1.getView(MessageThread.java:65)
            at android.widget.AbsListView.obtainView(AbsListView.java:2836)
            at android.widget.ListView.makeAndAddView(ListView.java:1903)
            at android.widget.ListView.fillSpecific(ListView.java:1364)
            at android.widget.ListView.layoutChildren(ListView.java:1698)
            at android.widget.AbsListView.onLayout(AbsListView.java:2640)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2414)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1264)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6943)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
     Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at com.example.clyde.listviewtest.MessageThread$1.getView(MessageThread.java:65)
            at android.widget.AbsListView.obtainView(AbsListView.java:2836)
            at android.widget.ListView.makeAndAddView(ListView.java:1903)
            at android.widget.ListView.fillSpecific(ListView.java:1364)
            at android.widget.ListView.layoutChildren(ListView.java:1698)
            at android.widget.AbsListView.onLayout(AbsListView.java:2640)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2414)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1264)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6943)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

1 个答案:

答案 0 :(得分:0)

如果您只想要textView的重力,那么您可以使用标准ArrayAdapterhere作为答案:

<TextView
    android:id="@+id/msgThread"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxWidth="1000px"
    android:gravity="right"/>

如果您希望TextView的宽度为wrap_content,则应为this帖子中的列表项创建新的ArrayAdapter和自己的布局。我会在这里写下差异,你的row.xml应该是这样的:

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

    <TextView
        android:id="@+id/msgThread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="1000px"
        android:layout_alignParentRight="true"
        android:gravity="right"/>
</RelativeLayout>