java - 以编程方式设置 PercentageRelativeLayout 中的宽高比

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

我想以编程方式设置 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();

关于java - 以编程方式设置 PercentageRelativeLayout 中的宽高比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897493/

相关文章:

java - 如何在 Java 中将 boolean 条件转换为整数类型而不在已编译的字节码和 JITed 机器码中进行分支或跳转

java - 从文本文件输入创建对象

android - 调试 FFmpeg

android - 内存不足内存泄漏问题

Android ViewPager 上一页和下一页可见吗?

android - 如果彼此相等则更改 textViews

java - 无法在 Android Studio 中运行应用程序。空指针异常

java - 在 java 中将 xml 配置文件用于我的应用程序的最佳方法是什么?

java - Dropwizard 应用程序中出现断管异常

android - 如何激活 TextView 上的文本选取框?