我想查看textview的高度和textview的填充值之和。 我設置填充與方法:獲取身高和填充大小
textView.setPadding(0,x,0,x);
但是,如果使用
textView.getHeight();
我會收到0
如果我使用
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
Log.d(TAG,params.height+"");
我會收到-2那不可能。
我該怎麼辦?
編輯:這是XML代碼
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/try"
android:textColor="#fff"
android:id="@+id/tv"
android:gravity="center_horizontal" />
EDIT2:我用這個代碼來設置文本視圖的寬度和它correctely工作。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
params.width = width/2;
我不想因爲我設置了填充而編程設置高度。但我想要得到的測量
EDIT3:
我嘗試使用
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content
爲Slartibartfast告訴我的。但有一個問題:
可能這是文本大小的測量...但它不是正確的測量高度的textview。我試圖總結此數量和2測量的填充並嘗試設置:
params.height = bounds.height() + 2*x;
的視圖是比沒有這行代碼的視圖小。
-2通常意味着你已經使用MATCH_PARENT或WRAP_CONTENT(這些被賦值爲con stants)作爲高度屬性。你還可以粘貼textview的xml嗎? – 2015-02-23 09:24:01
完成編輯!這是我的代碼!有可能在wrap_content視圖中計算真實高度? – pippo15 2015-02-23 09:37:29