简体   繁体   中英

How to detect width of TextView that has attribute wrap_content?

I have a layout with element TextView

<TextView android:textColor="#ffffff" android:text="18 августа"
                    android:id="@+id/program_day1" android:layout_gravity="center"
                    android:textSize="18sp" android:textStyle="bold"
                    android:layout_height="wrap_content" android:layout_width="wrap_content"
                    android:layout_weight="1" android:gravity="center" />

How can I detect the real size (and margins) of this in the onStart (or other) event? Now it shows "-2" values

try with myTextView.getPaint().measureText(myTextViewtext) it didn't return the size of the view but the size of the text,hope it could help you

Views don't get height or width until they are actually drawn on screen.

Try to show the width in the onSizeChanged function. Check if a real size will be shown. Use getwidth .

Apart from that, you can use TextView.getWidth() and Textview.getHeight() to get w/h and
LayoutParams lp = (LayoutParams) TextView.getLayoutParams(); to get margin values

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