detect clipping in android TextView

前端 未结 3 1603
天命终不由人
天命终不由人 2021-02-09 04:12

I have a TextView in my android application that has a set width on it. It\'s currently got a gravity of \"center_horitonzal\" and a set textSize (9sp). I pull values to put o

3条回答
  •  你的背包
    2021-02-09 04:54

    Another way to do this (which might be equivalent) is something like this:

            TextView title = new TextView(context) {
                @Override
                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                    int textsize = 30;
                    setTextSize(textsize);
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                    while (getMeasuredHeight() > MeasureSpec.getSize(heightMeasureSpec)) {
                        textsize--;
                        setTextSize(textsize);
                        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                    }
                }
            };
    

    Just a warning that this seems to work, but I just threw it together - might be some edge cases in ways the TextView can be measured.

提交回复
热议问题