简体   繁体   中英

How to know if there is available space in TextView?

Because I would like to make the same, like Whatsapp, it puts message and hour in the same line

在此输入图像描述

or in another line.

Sometimes there is space and Whatsapp puts the hour in same line. However sometimes there is not space and Whatsapp puts the hour in other line.

Inside or outside...

Any idea?

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

Return the number of lines of text in this layout.

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

I think you can give textView.getLayout().getLineWidth(lineNum) a try ( lineNum is obviously the last line). Then compare it to the parent layout width, and if the difference is bigger than timestamp.getWidth() plus some extra padding, then you display it on the same line.

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