Android getMeasuredHeight returns wrong values !

后端 未结 7 678
野的像风
野的像风 2020-12-02 20:13

I\'m trying to determine the real dimension in pixels of some UI elements !

Those elements are inflated from a .xml file and are initialized with dip width and height

相关标签:
7条回答
  • 2020-12-02 20:52

    You have to create Custom Textview and use it in your layouts and use getActual height function to set the height at runtime

    public class TextViewHeightPlus extends TextView {
        private static final String TAG = "TextViewHeightPlus";
        private int actualHeight=0;
    
    
        public int getActualHeight() {
            return actualHeight;
        }
    
        public TextViewHeightPlus(Context context) {
            super(context);
        }
    
        public TextViewHeightPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
            setCustomFont(context, attrs);
        }
    
        public TextViewHeightPlus(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
        }
    
       @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            actualHeight=0;
    
            actualHeight=(int) ((getLineCount()-1)*getTextSize());
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题