[英]Set aspect ratio in PercentageRelativeLayout programmatically
我想以编程方式在PercentRelativeLayout
为我的FrameLayout
设置纵横比。
我已经在xml
尝试过,它正在工作:
<android.support.percent.PercentRelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%" //aspect ratio
android:orientation="vertical"
android:id="@+id/liveTV_frame"
android:layout_marginBottom="50dp"
android:layout_centerInParent="true"
android:background="@android:color/background_dark"
android:keepScreenOn="true">
</FrameLayout>
</android.support.percent.PercentRelativeLayout>
但是我需要以编程方式进行设置。
我怎样才能做到这一点?
PercentRelativeLayout layout = ...;
PercentRelativeLayout.LayoutParams layoutParams
= (PercentRelativeLayout.LayoutParams) layout.getLayoutParams();
layoutParams.getPercentLayoutInfo().aspectRatio = ...; // float value
layout.requestLayout();
来自@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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.