0

RelativeLayout.ALIGN_PARENT_RIGHT 不起作用。(((这是我的Android-Function。她在舞台上添加了新消息。

我不明白。为什么我的代码不能正常工作

        private void addMessageToStage(Message message) {

            LinearLayout scrollChatMessagesOutput = (LinearLayout) findViewById(R.id.chatMessagesOutput);

            LayoutInflater layoutInflater = getLayoutInflater();

            View view = layoutInflater.inflate(R.layout.chat_single_message, null, false);

            TextView messageTextView = (TextView) view.findViewById(R.id.singleChatMessageTextView);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

            if (messagesAlignId % 2 == 0) {

                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

                    messageTextView.setBackgroundResource(R.drawable.chat_input_message_shape);

            } else {

                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

                    messageTextView.setBackgroundResource(R.drawable.chat_output_message_shape);

            }

            messageTextView.setText(Html.fromHtml("<b><u>" + message.getMessage() + "<font color=\"#cccccc\" size=\"4px\">" + "\n" + message.getTime() + "</font></b></u>"));

            RelativeLayout messageAlignLayout = new RelativeLayout(this);

            messageAlignLayout.addView(view, params);

            scrollChatMessagesOutput.addView(messageAlignLayout);

    }

它是chat_single_message。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/chatMessage"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:weightSum="1.0" >
    <TextView
       android:id="@+id/singleChatMessageTextView"
       style="@style/singleChatMessageTextStyle" />
</LinearLayout>

它是 singleChatMessageTextStyle。

<style name="singleChatMessageTextStyle" parent="@android:style/TextAppearance.Small">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:layout_marginLeft">5dp</item>
    <item name="android:layout_marginRight">5dp</item>
    <item name="android:padding">3dp</item>
    <item name="android:layout_weight">0.7</item>
    <item name="android:textColor">#000000</item>
    <item name="android:typeface">sans</item>
    <item name="android:textSize">12sp</item>
</style>
4

1 回答 1

0

考虑使用两种不同的布局:

  • 左侧文本的布局,chat_single_message_left.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/chatMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1.0" >
    
        <TextView
            android:id="@+id/singleChatMessageTextView"
            style="@style/singleChatMessageTextStyle" />
    
        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3" 
            android:visibility="invisible" />
    
    </LinearLayout>
    
  • 与右侧文本类似的布局chat_single_message_right.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/chatMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1.0" >
    
        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3" 
            android:visibility="invisible" />
    
        <TextView
            android:id="@+id/singleChatMessageTextView"
            style="@style/singleChatMessageTextStyle" />
    
    </LinearLayout>
    

您应该使用 ListView 而不是 ScrollView 来显示每条消息,ListView对于大型对话来说要快得多

于 2012-11-30T20:52:08.653 回答