0

我有下面的代码,我想让时间戳小于文本的其余部分,但已经努力了 2 天才能让它工作。

代码:

private void alignTheMessageHistory(String username, String message, String sentdt) {

    StringBuffer sBuffer = new StringBuffer();
//  if (username.equals(friend.userName)) {
//      sBuffer.append(username + ":\n");
//  }
    sBuffer.append(message + "\n");
    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    topParams.setMargins(10, 10, 10, 10);
    layout.setLayoutParams(topParams);

    TextView valueTV = new TextView(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

    if (username.equals(friend.userName)) {
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        valueTV.setBackgroundResource(R.drawable.left_gray);
        valueTV.setPadding(15, 5, 10, 5);

    } else {
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        valueTV.setBackgroundResource(R.drawable.right_blue);
        valueTV.setPadding(10, 5, 15, 5);

    }

    params.setMargins(0, 5, 0, 0);
    sBuffer.append(((sentdt == null) 
            ? java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) 
            : sentdt)); ////////////////////////////NEED THIS TO BE SMALLER


    valueTV.setLayoutParams(params);
    valueTV.setText(sBuffer);
    //
    valueTV.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            ((ScrollView) findViewById(R.id.scrollView))
                    .fullScroll(View.FOCUS_DOWN);
        }
    });
    layout.addView(valueTV);
    messageHistoryText.addView(layout);
}
4

1 回答 1

0

您可以使用SpannablesHtml.fromHtml. 关于这个主题已经有一些 SO 问题。

这是使用Html该类的示例:

String s = sentdt;
if(s == null) {
    s = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
}
valueTV.setText( Html.fromHtml(Html.escapeHtml(message) + "<br><small>" + s + "</small>") );
于 2013-11-09T23:29:20.237 回答