我在以编程方式创建textView时遇到一些问题.我真的不知道如何解决它.

问题是:
- 保证金,我不希望我的TextView相互粘连.
- 文本不像标题中那样位于drawable的中心
- 我想让Nickname用户的消息向右对齐,Nick的消息对齐到左边.
为此我有这两个功能:
private void appendSenderText(String message) {
TextView msg = new TextView(ChatActivity.this);
msg.setBackgroundResource(R.drawable.rectangle);
msg.setText(message);
msg.setPadding(10, 10, 10, 10);
msg.setTextColor(getResources().getColor(R.color.white));
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.LEFT);
params.setMargins(10, 10, 10, 10);
msg.setLayoutParams(params);
LinearLayout chat = (LinearLayout) findViewById(R.id.chatLayout);
chat.addView(msg);
}
private void appendReceiverText(String message) {
TextView msg = new TextView(ChatActivity.this);
msg.setBackgroundResource(R.drawable.rectangle);
msg.setText(message);
msg.setPadding(10, 10, 10, 10);
msg.setTextColor(getResources().getColor(R.color.white));
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
params.setMargins(10, 10, 10, 10);
msg.setLayoutParams(params);
LinearLayout chat = (LinearLayout) findViewById(R.id.chatLayout);
chat.addView(msg);
}
Run Code Online (Sandbox Code Playgroud)
它似乎无法正常工作.我还检查了函数是否被正确调用.我注意到在标题的XML文件中,我指定了layout_gravity而不是gravity. …