【问题标题】:Android PercentRelativeLayout width won't change with ratio when layout changed布局更改时,Android PercentRelativeLayout 宽度不会随比例变化
【发布时间】:2016-06-07 22:52:14
【问题描述】:

我正在尝试让纹理视图在布局更改时保持 16:9 的比例。 我使用 percentrelativelayout 来实现它。 另外我需要纹理视图在宽度/高度上都不要超过屏幕尺寸。所以我使用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

【问题讨论】:

    标签: android android-percentrelativelayout


    【解决方案1】:

    我不得不使用这些 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"
                />
    

    【讨论】:

    • 嗨乔尔,感谢您的回复。我尝试设置与 urs 相同的布局(即使使用 framelayout 而不是 textureview)但仍然得到相同的结果。我想办法让两个纹理视图在其他布局可见性发生变化时切换。 (在显示/隐藏另一种布局之前隐藏旧布局,然后在之后显示新布局)。有用。但还是得看看我的activity中能否轻松处理2个textureview。
    • "确保将 PercentRelativeLayout 嵌入到常规的 RelativeLayout" 为什么会这样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多