布局更改时,Android PercentRelativeLayout宽度不会随比例变化

时间:2016-06-07 22:52:14

标签: android android-percentrelativelayout

我正在尝试使layoutview在布局更改时保持16:9的比例。 我使用percentrelativelayout来实现它。 另外,我需要在宽度/高度上不超过屏幕尺寸的textureview。所以我使用2层来确保它不会超过屏幕。

它在android studio的布局预览中完美调整大小。但宽度不会改变,以保持设备在运行时的比率。

这是布局xml和屏幕截图。

有人遇到同样的问题吗?

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center">

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/percent"
        app:layout_aspectRatio="178%"
        app:layout_widthPercent="100%"
        android:background="@android:color/darker_gray">
        <TextureView
            android:id="@+id/texture"
            app:layout_aspectRatio="178%"
            app:layout_heightPercent="100%"
            android:layout_centerInParent="true">
        </TextureView>
    </android.support.percent.PercentRelativeLayout>

</android.support.percent.PercentRelativeLayout>

One layout in the bottom is set to VISIBLE

After bottom layout set to GONE

1 个答案:

答案 0 :(得分:0)

我不得不玩这些PercentRelativeLayouts来获得我正在寻找的结果。下面是我用来获得你想要的相同结果的代码,除了我使用了一个片段。我已经在多个设备上测试了它,它可以处理所有不同的屏幕尺寸。在你的情况下,我建议用你的TextureView替换我的“article_preview_frame”FrameLayout,看看它能带给你什么。确保将PercentRelativeLayout嵌入到常规RelativeLayout中。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/article_preview_relative_layout"
>

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/article_preview_container">

    <FrameLayout
        app:layout_widthPercent="100%"
        app:layout_aspectRatio="178%"
        android:layout_alignParentTop="true">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/article_preview_frame"
            />