Whatsapp Message Layout - How to get time-view in the same row

后端 未结 12 1106
执笔经年
执笔经年 2020-11-28 03:18

I was wondering how WhatsApp handles the time shown in every message.

For those who don\'t know:

  1. If the message is very short, the text and time are in
12条回答
  •  不知归路
    2020-11-28 03:57

    I propose another solution

    public static final String TAG = "MainActivity";
        private TextView mText;
        private RelativeLayout relativeLayout;
        private Boolean mFirstTime = true;
        private static final int WIDH_HOUR = 382;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            final int width = getScreensWidh();
    
            mText = (TextView) findViewById(R.id.activity_main_text);
            relativeLayout = (RelativeLayout) findViewById(R.id.activity_main_relative);
    
            mText.setText("aaaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa dsa aaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa");
    
            ViewTreeObserver vto = mText.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (mFirstTime) {
                        Layout layout = mText.getLayout();
                        int lines = layout.getLineCount();
    
                        int offset = layout.layout.getLineWidth(lines - 1);
                        int freeSpace = width - offset;
    
                        TextView hour = new TextView(MainActivity.this);
                        hour.setText("12:20");
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                        if (freeSpace > WIDH_HOUR) {
                            params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.activity_main_text);
                        } else {
                            params.addRule(RelativeLayout.BELOW, R.id.activity_main_text);
                        }
                        hour.setLayoutParams(params);
                        relativeLayout.addView(hour);
                        Log.d(TAG, String.valueOf(freeSpace));
                        mFirstTime = false;
                    }
    
                }
            });
    
    
        }
    
        public int getScreensWidh() {
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            return size.x;
    
        }
    

    Two Public Methods

    • public abstract int getLineCount ()

    Return the number of lines of text in this layout.

    • public int getLineWidth(int line)

    Gets the unsigned horizontal extent of the specified line, including leading margin indent and trailing whitespace.

提交回复
热议问题