1

可以使用PercentRelativeLayout僅指定所需的寬度尺寸並自動調高?使用僅指定寬度的PercentRelativeLayout(高度自動)

我的意思是這樣:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
    android:id="@+id/earthImageView" 
    app:layout_widthPercent="30%" 
    android:src="@drawable/earth" 
    android:layout_alignParentRight="true" 
    android:adjustViewBounds="true"/> 
</android.support.percent.PercentRelativeLayout > 

的觀點有正確的寬度,但如果我檢查的佈局界限,我可以看到,高度爲MATCH_PARENT。這意味着帽子android:adjustViewBounds =「true」不起作用。

我嘗試添加應用程序:layout_widthPercent =「WRAP_CONTENT」,但沒有效果。

謝謝。

編輯:

ASLO做逆情況時有問題:

<ImageView 
     android:id="@+id/earthImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     app:layout_heightPercent="20%" 
     android:src="@drawable/earth" 
     android:adjustViewBounds="true"/> 

回答

1

如果我理解正確的話,你要指定的寬度,並有自動設置爲任何adjustViewBounds高度給你。

如果是這樣的話,下面應該做你想做的(我同時設置佈局和ImageView的高度爲wrap_content):

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/earthImageView" 
     android:layout_height="wrap_content" 
     app:layout_widthPercent="30%" 
     android:src="@drawable/lapin" 
     android:layout_alignParentRight="true" 
     android:adjustViewBounds="true"/> 
</android.support.percent.PercentRelativeLayout > 
+0

似乎工作!謝謝! – NullPointerException

+0

順便說一句,如果我試着它與高度百分比,而不是寬度...我得到了同樣的錯誤。我在問題中添加了新問題,請你測試一下嗎? – NullPointerException

+0

這是一個很好的問題@NullPointerException,似乎無法使其工作。你知道你的形象比例嗎?如果是這樣,你可以刪除layout_width(和layout_height)並使用'app:layout_aspectRatio =「80%」'例如 – mVck