TextView的Andr​​oid的编程 [英] TextView programmatically Android

查看:147
本文介绍了TextView的Andr​​oid的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题编程方式创建一个TextView。我真的不知道如何解决它。

的问题是:

-The缘,我不希望我的TextView粘到对方。

-The文本没有在绘制在标题居中像

-I要昵称用户的对准在正确的消息和尼克的消息靠左对齐。

对于这个我有这两个功能:

 私人无效appendSenderText(字符串消息){        TextView的味精=新的TextView(ChatActivity.this);
        msg.setBackgroundResource(R.drawable.rectangle);
        msg.setText(消息);
        msg.setPadding(10,10,10,10);
        msg.setTextColor(getResources()的getColor(R.color.white));
        FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT);
        params.setMargins(10,10,10,10);
        msg.setLayoutParams(PARAMS);
        聊天的LinearLayout =(的LinearLayout)findViewById(R.id.chatLayout);
        chat.addView(MSG);
    }    私人无效appendReceiverText(字符串消息){        TextView的味精=新的TextView(ChatActivity.this);
        msg.setBackgroundResource(R.drawable.rectangle);
        msg.setText(消息);
        msg.setPadding(10,10,10,10);
        msg.setTextColor(getResources()的getColor(R.color.white));
        FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
        params.setMargins(10,10,10,10);
        msg.setLayoutParams(PARAMS);
        聊天的LinearLayout =(的LinearLayout)findViewById(R.id.chatLayout);
        chat.addView(MSG);
    }

看来,这是行不通的。我还检查了函数正确调用。
我注意到,在为标题我的XML文件,我指定layout_gravity而不是重力。

编辑:我使用的ListView如你所说,一切工作正常,但我仍然有文字这个问题不集中,虽然我用 msg.setGravity(gravity.CENTER);
也许我的绘有问题。

下面是我的圆润rectanngle XML文件。这是奇怪的,当我创建我的XML文件中的TextView,文字居中,而当我想创建编程,它不是这样的。

下面是我绘制的code。

 <?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形>
    <梯度
     机器人:startColor =@彩色/ drk_button
     机器人:ENDCOLOR =@彩色/ lgt_button
     机器人:角=90>
   < /梯度GT;   <角落的an​​droid:半径=7dip/>   <中风
     机器人:宽=1px的
     机器人:颜色=@色/ drk_button/>
< /形状>

和这里的code,我创建texView。

 消息消息=(消息)this.getItem(位置);        ViewHolder持有人;
        如果(convertView == NULL)
        {
            持有人=新ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row,父母,假);
            holder.message =(TextView中)convertView.findViewById(R.id.message_text);
            convertView.setTag(保持器);
        }
        其他
            支架=(ViewHolder)convertView.getTag();        holder.message.setText(message.getMessage());
        holder.message.setTextSize(17);
        holder.message.setGravity(Gravity.CENTER);
        的LayoutParams LP =(的LayoutParams)holder.message.getLayoutParams();
        如果(message.isMine())
        {
            holder.message.setBackgroundResource(R.drawable.rectangle);
            lp.gravity = Gravity.LEFT;
        }
        其他
        {
            holder.message.setBackgroundResource(R.drawable.rectangledest);
            lp.gravity = Gravity.RIGHT;
        }
        holder.message.setLayoutParams(LP);
        holder.message.setTextColor(Color.WHITE);        返回convertView;
    }


解决方案

我更新了你的职责,它是工作作为你现在想:

 私人无效appendSenderText(字符串消息){    TextView的味精=新的TextView(ButtonsActivity.this);
    msg.setBackgroundResource(R.drawable.rectangle);
    msg.setText(消息);
    msg.setPadding(10,10,10,10);
    msg.setTextColor(getResources()的getColor(R.color.white));
    LinearLayout.LayoutParams PARAMS =新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 =(的LinearLayout)findViewById(R.id.chatLayout);
    chat.addView(MSG);
}私人无效appendReceiverText(字符串消息){    TextView的味精=新的TextView(ButtonsActivity.this);
    msg.setBackgroundResource(R.drawable.rectangle);
    msg.setText(消息);
    msg.setPadding(10,10,10,10);
    msg.setTextColor(getResources()的getColor(R.color.white));
    LinearLayout.LayoutParams PARAMS =新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 =(的LinearLayout)findViewById(R.id.chatLayout);
    chat.addView(MSG);
}

您必须设置的TextView 重力里面的文字的TextView 定位和父<$设置重力C $ C>的LinearLayout 设置的TextView 里面的的LinearLayout 的位置。

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

I have some problems creating a textView programmatically. I don't really know how to solve it.

The problems are :

-The margin, I don't want my TextView stuck to each other.

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

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.

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.

Here is the code of my 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.

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

解决方案

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.

Notice that I'm using a LinearLayout instead of a FrameLayout.

这篇关于TextView的Andr​​oid的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆