RelativeLayout中的Android Textview宽度

时间:2012-03-30 03:53:05

标签: android android-layout

我正在写一个聊天程序。在聊天活动中,我发送和接收的消息显示为像Bubble一样的EditTexts。我想设置我的edittexts的宽度与其消息的宽度相同。在我的代码中,当创建消息时,将在relativelayout中以编程方式创建新的edittexts。我该怎么设置它?

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

}

功能就是这个。我不想设置宽度fixsize。解决方案是什么?

1 个答案:

答案 0 :(得分:1)

这样的事情......?

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