小编nyt*_*hin的帖子

TextView以编程方式安装Android

我在以编程方式创建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. …

android textview programmatically-created

4
推荐指数
1
解决办法
2万
查看次数