【问题标题】:How to set the percentage height/width of child views of PercentFrameLayout at runtime如何在运行时设置 PercentFrameLayout 子视图的百分比高度/宽度
【发布时间】:2016-11-29 16:02:58
【问题描述】:

我想以编程方式设置 PercentFrameLayout 的子视图的高度/宽度。如何在运行时设置 PercentFrameLayout 子视图的百分比高度/宽度..

【问题讨论】:

    标签: android


    【解决方案1】:

    我假设您的 PercentLayout 中有 ID 为 view_child 的 View。

    所以使用这个代码sn-p

    View view = findViewById(R.id.view_child);
    PercentRelativeLayout.LayoutParams params =(PercentRelativeLayout.LayoutParams) view.getLayoutParams();
    // This will currently return null, if view not exist in your xml.
    PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
    info.heightPercent = 0.60;
    view.requestLayout();
    

    希望对您有所帮助..

    【讨论】:

      【解决方案2】:
      ImageView child= new ImageView(getActivity());
      
      percentRelativeLayout.addView(child);
      
      PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) child.getLayoutParams();
      
      PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
      
      info.heightPercent = 0.05f;
      info.widthPercent = 0.05f;
      info.leftMarginPercent = 0.5f;
      child.setLayoutParams(params);
      

      【讨论】:

      • 嗨,我试过了。但对我来说 PercentLayoutInfo 总是返回 null。你知道为什么吗?
      猜你喜欢
      • 2012-04-05
      • 2013-03-13
      • 2014-02-17
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2012-12-17
      相关资源
      最近更新 更多