简体   繁体   English

如何检测具有 wrap_content 属性的 TextView 的宽度?

[英]How to detect width of TextView that has attribute wrap_content?

I have a layout with element TextView我有一个带有元素 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?如何在 onStart (或其他)事件中检测到它的实际大小(和边距)? Now it shows "-2" values现在它显示“-2”值

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尝试使用 myTextView.getPaint().measureText(myTextViewtext) 它没有返回视图的大小,而是返回文本的大小,希望它可以帮助你

Views don't get height or width until they are actually drawn on screen.视图在实际绘制在屏幕上之前不会获得高度或宽度。

Try to show the width in the onSizeChanged function.尝试在onSizeChanged function 中显示宽度。 Check if a real size will be shown.检查是否会显示真实尺寸。 Use getwidth .使用getwidth

Apart from that, you can use TextView.getWidth() and Textview.getHeight() to get w/h and除此之外,您可以使用TextView.getWidth()Textview.getHeight()来获取 w/h 和
LayoutParams lp = (LayoutParams) TextView.getLayoutParams(); to get margin values获取保证金值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM