【问题标题】:Android calculate height of child View When overflowAndroid计算溢出时子视图的高度
【发布时间】:2016-01-06 16:42:38
【问题描述】:

我嵌套了LinearLayouts,其父高度为90,但子视图可能根据其中设置的文本具有不同的高度,并且可能会溢出其父视图。我想将父动画设置为子的最终高度,但是子 getHeight() 和父 getMeasuredHeight() 的两种方法,返回 90400

parent.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
targetHeight = parent.getMeasuredHeight();
//returns 90

还有

View child=((ViewGroup) parent).getChildAt(0);
int targetHeight=child.getHeight();
//returns 90

【问题讨论】:

    标签: android height overflow


    【解决方案1】:

    试试这个代码。希望它会工作。

    ViewGroup.LayoutParams params = yourView.getLayoutParams();
    int height = params.height;
    

    【讨论】:

      【解决方案2】:
          LayoutParams paramsChild = child.getLayoutParams();
      
          LayoutParams paramsParent = parent.getLayoutParams();
          paramsParent.height = paramsChild.height;
          parent.setLayoutParams(paramsParent);
      

      【讨论】:

        猜你喜欢
        • 2013-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-15
        • 2021-09-04
        • 2013-05-15
        • 1970-01-01
        相关资源
        最近更新 更多