简体   繁体   中英

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. I want to set my width of edittexts with the same width of its messages. In my code, when message is created, new edittexts are being created programmatically in the relativelayout. 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. What is the solution?

像这样..?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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