以编程方式在PercentageRelativeLayout中设置纵横比

时间:2017-03-20 06:57:00

标签: java android android-layout android-view android-percentrelativelayout

我想以编程方式在from内设置 $res = $this->db->get('account'); 的宽高比。

我已经尝试了FrameLayout,它正在工作:

PercentRelativeLayout

但我需要以编程方式设置它。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

PercentRelativeLayout layout = ...;
PercentRelativeLayout.LayoutParams layoutParams 
           = (PercentRelativeLayout.LayoutParams) layout.getLayoutParams();
layoutParams.getPercentLayoutInfo().aspectRatio = ...; // float value
layout.requestLayout();

答案 1 :(得分:0)

来自@azizbekian回答我的回答,

不使用分数,设置百分比浮动值:

 PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) mFlPromoVideoContainer.getLayoutParams();
        PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
        info.widthPercent = 1f;
        info.aspectRatio = 1.78f;//178%
        mFlPromoVideoContainer.requestLayout();

您可以在XML中指定分数:

<item name="aspect_ratio" type="fraction">178%</item>

在Java中,

 PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) mFlPromoVideoContainer.getLayoutParams();
        PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
        info.widthPercent = 1f;
        info.aspectRatio = getResources().getFraction(R.fraction.aspect_ratio, 1, 1);
        mFlPromoVideoContainer.requestLayout();