TextView programmatically Android

╄→гoц情女王★ 提交于 2020-01-01 11:59:28

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/23990743/textview-programmatically-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!