android - Cannot set TextView width using setWidth or setLayoutParams -
Note: The original question was edited where I was under the impression bound incorrect values
I am trying to include a full text programmatically to detect the width of the text. I have the following code, setWidth () and setLayoutParams do not work.
TextView textView = new TextView (getActivity ()); String text = "my text"; TextView.setText (text); Paint paint = new paint (); Rectangle border = new rack (); Paint.setTypeface (textView.getTypeface ()); Paint.setTextSize (textView.getTextSize ()); Paint.getTextBounds (text, 0, text length (), border); TextView.setWidth (bounds.width ()); TextView.setHeight (bounds.height ()); Tried to use LayoutParams , but there is still no luck.
ViewGroup LayoutParams params = newViewGroup.LayoutParams (ViewGroup.LayoutParamsWRAP_CONTENT, 75); TextView.setLayoutParams (parameter); Generally there are things when a function uses pixels, and uses other DPI.
But why do you need to measure the text manually on earth? Typically people specify wrap_content in xcode.
Example:
& lt; Button Android: Text = "Press Me!" Android: layout_width = "fill_parrent" android: layout_height = "rap_sign" android: onClick = "onButton1" /> & Lt ;! - Android: layout_width = "wrap_content" - & gt; Read about the layout again: and try using getMeasuredWidth () and getMeasuredHeight (); If you can not be sure that the layout has already happened, then use the visual tree supervisor and its callback.
Comments
Post a Comment