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>