TextView 以编程方式 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23990743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextView programmatically Android
提问by tonystrawberry
I have some problems creating a textView programmatically. I don't really know how to solve it.
我在以编程方式创建 textView 时遇到了一些问题。我真的不知道如何解决它。
The problems are :
问题是:
-The margin, I don't want my TextView stuck to each other.
- 边距,我不希望我的 TextView 粘在一起。
-The text is not centered in the drawable like in the title
- 文本不像标题那样在可绘制对象中居中
-I want the messages of Nickname user aligned to the right and the messages of Nick aligned to the left.
- 我希望昵称用户的消息向右对齐,尼克的消息向左对齐。
For this I have these two functions :
为此,我有这两个功能:
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);
}
It seems that it is not working. I've also checked that the functions are correctly called. I noticed that in my XML file for the title, I specified layout_gravity and not gravity.
它似乎不起作用。我还检查了这些函数是否被正确调用。我注意到在标题的 XML 文件中,我指定了 layout_gravity 而不是重力。
EDIT : I used ListView as you suggested, everything is working fine but I still have this problem with the text not centered, although I used msg.setGravity(gravity.CENTER);
Maybe my drawable has a problem.
编辑:我按照您的建议使用了 ListView,一切正常,但我仍然遇到文本未居中的问题,尽管我使用了msg.setGravity(gravity.CENTER);
也许我的 drawable 有问题。
Here is the xml file for my rounded rectanngle. That's weird, when I create my Textview in XML file, the text is centered, and when I want to create programmatically, it's not the case.
这是我的圆形矩形的 xml 文件。这很奇怪,当我在 XML 文件中创建 Textview 时,文本居中,而当我想以编程方式创建时,情况并非如此。
Here is the code of my drawable.
这是我的drawable的代码。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/drk_button"
android:endColor="@color/lgt_button"
android:angle="90">
</gradient>
<corners android:radius="7dip" />
<stroke
android:width="1px"
android:color="@color/drk_button" />
</shape>
And here the code where I create the texView.
这里是我创建 texView 的代码。
Message message = (Message) this.getItem(position);
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.message_text);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.message.setText(message.getMessage());
holder.message.setTextSize(17);
holder.message.setGravity(Gravity.CENTER);
LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
if(message.isMine())
{
holder.message.setBackgroundResource(R.drawable.rectangle);
lp.gravity = Gravity.LEFT;
}
else
{
holder.message.setBackgroundResource(R.drawable.rectangledest);
lp.gravity = Gravity.RIGHT;
}
holder.message.setLayoutParams(lp);
holder.message.setTextColor(Color.WHITE);
return convertView;
}
回答by joao2fast4u
I updated your functions and it is working as you want now:
我更新了您的功能,现在可以正常工作了:
private void appendSenderText(String message) {
TextView msg = new TextView(ButtonsActivity.this);
msg.setBackgroundResource(R.drawable.rectangle);
msg.setText(message);
msg.setPadding(10, 10, 10, 10);
msg.setTextColor(getResources().getColor(R.color.white));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(5, 15, 0, 0);
params.gravity = Gravity.LEFT;
msg.setLayoutParams(params);
msg.setGravity(Gravity.CENTER);
LinearLayout chat = (LinearLayout) findViewById(R.id.chatLayout);
chat.addView(msg);
}
private void appendReceiverText(String message) {
TextView msg = new TextView(ButtonsActivity.this);
msg.setBackgroundResource(R.drawable.rectangle);
msg.setText(message);
msg.setPadding(10, 10, 10, 10);
msg.setTextColor(getResources().getColor(R.color.white));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 15, 5, 0);
params.gravity = Gravity.RIGHT;
msg.setLayoutParams(params);
msg.setGravity(Gravity.CENTER);
LinearLayout chat = (LinearLayout) findViewById(R.id.chatLayout);
chat.addView(msg);
}
You have to set TextView
gravity to position the text inside the TextView
and set gravity in the parent LinearLayout
to set the position of the TextView
inside the LinearLayout
.
你必须设置TextView
重力里面的文字定位TextView
和重心设置在父LinearLayout
设置的位置TextView
内LinearLayout
。
Notice that I'm using a LinearLayout
instead of a FrameLayout
.
请注意,我使用的是 aLinearLayout
而不是FrameLayout
。
回答by AndEngine
Use custom listviews to populate chat lines.
使用自定义列表视图填充聊天行。
To make the chat text at center add this line.
要使聊天文本居中,请添加此行。
msg.setGravity(Gravity.CENTER);
I believe your frame layout brings the textviews close each other.
我相信您的框架布局使文本视图彼此接近。
I recommend you to use ListView with custom adapter to populate chat lines. Its easy to add the chat lines and update the view in ListViews. check this link. it will give you a basic idea.
我建议您使用带有自定义适配器的 ListView 来填充聊天行。在 ListViews 中添加聊天行和更新视图很容易。检查此链接。它会给你一个基本的想法。