gpt4 book ai didi

android - Android 编程方式的 TextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:14 25 4
gpt4 key购买 nike

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

enter image description here

问题是:

-边距,我不希望我的 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);
}

好像不行。我还检查了函数是否被正确调用。我注意到在我的标题 XML 文件中,我指定了 layout_gravity 而不是 gravity。

编辑:我按照您的建议使用了 ListView,一切正常,但我仍然遇到文本未居中的问题,尽管我使用了 msg.setGravity(gravity.CENTER);也许我的 drawable 有问题。

这是我的圆角矩形的 xml 文件。这很奇怪,当我在 XML 文件中创建我的 Textview 时,文本居中,而当我想以编程方式创建时,情况并非如此。

enter image description here

这是我的可绘制对象的代码。

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

这里是我创建 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;
}

最佳答案

我更新了你的函数,它现在可以正常工作了:

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);
}

您必须设置 TextView gravity 以将文本定位在 TextView 中,并在父级 LinearLayout 中设置 gravity 以设置LinearLayout 内的 TextView

请注意,我使用的是 LinearLayout 而不是 FrameLayout

关于android - Android 编程方式的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23990743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com