取得 view 的實際所佔寬度。
getMeasuredWidth()
view 在 parent 估計會佔的寬度,是最近一次在 measure() 中計算的值。
Solution1
final TextView tv = (TextView)findViewById(R.id.image_test); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { LayerDrawable ld = (LayerDrawable)tv.getBackground(); ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });
Solution2
[AndroidLayout] getWidth() and getHeight()
* Reference
- getWidth和getMeasuredWidth的正解 - rui313的专栏 - 博客频道 - CSDN.NET
- android - How do you to retrieve dimensions of a view? Getheight() and Getwidth() always return zero - Stack Overflow
- Android初级教程_获取Android控件的宽和高 **
沒有留言:
張貼留言