【问题标题】:Find height of parent view查找父视图的高度
【发布时间】:2015-02-09 06:04:00
【问题描述】:

我正在创建一个自定义图像视图,并试图找到父级的高度。我知道的关于父母的唯一细节是它可能会滚动。我需要父母身高的原因是因为我试图找出图像视图在屏幕上的位置。我已经制作了一个可以准确计算其位置的方程式,但它仅在我手动输入父母身高时才有效。有什么方法可以检索此信息,或者是否有其他方法可以在每次更改时获取我的 imageview 在屏幕上的位置?

【问题讨论】:

    标签: android imageview android-custom-view


    【解决方案1】:

    试试这个方法

    public class MyImageView extends ImageView {
    
    
        int parentHeight;
        int parentWidth;
    
        public MyImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public MyImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyImageView(Context context) {
            super(context);
        }
    
    
    
    
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            if(((View)this.getParent()).getMeasuredWidth()!=0){
                parentHeight = ((View)this.getParent()).getMeasuredHeight();
                parentWidth = ((View)this.getParent()).getMeasuredWidth();
            }
    
        }
    }
    

    【讨论】:

    • 这看起来像是在工作,我如何确认它返回了正确的值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2013-10-28
    • 2016-12-08
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多