简体   繁体   English

RelativeLayout中的Android Textview宽度

[英]Android Textview width in RelativeLayout

I'm writing a chat program. 我正在写一个聊天程序。 In chat activity, my sent and received messages are displaying with EditTexts like bubbles. 在聊天活动中,我的已发送和已接收消息都显示有EditText,例如气泡。 I want to set my width of edittexts with the same width of its messages. 我想将Edittexts的宽度设置为其消息的宽度。 In my code, when message is created, new edittexts are being created programmatically in the relativelayout. 在我的代码中,创建消息时,将以编程方式在relativelayout中创建新的edittext。 How can I set it? 我该如何设置?

public void sendmyMessage(String msg){
    // mRR is my RelativeLayout
    RelativeLayout.LayoutParams params;
    EditText e = new EditText(mRR.getContext());
    e.setId(arr.size()+2);
    e.setEnabled(false);
    e.setFocusable(false);
    e.setClickable(false);
    e.setText(msg);
    e.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    e.setTypeface(null,Typeface.BOLD);
    e.setTypeface(Typeface.SANS_SERIF);
    e.setTextColor(Color.parseColor("#080808"));

    params = new RelativeLayout.LayoutParams(setDp(150, e.getContext()),RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.setMargins(0, setDp(8, e.getContext()), 0, 0);
    if(arr.isEmpty()){
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    }else{
        params.addRule(RelativeLayout.BELOW,arr.get(arr.size()-1).getId());
    }
    Spannable str = e.getText();
    str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    e.setLayoutParams(params);
    e.setBackgroundResource(R.drawable.mytext);
    arr.add(e);
    mRR.addView(e);

}

The function is this. 功能是这个。 I don't want to set the width fixsize. 我不想设置宽度fixsize。 What is the solution? 解决办法是什么?

像这样..?

editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM