2015-12-18 22 views
0

有沒有辦法將imageview尺寸設置爲顯示尺寸的一部分? 我有一張圖片,大概需要一半的屏幕尺寸,但我無法正確設置尺寸。在較小的屏幕(如Nexus 4)中看起來不錯,但在較大的屏幕(Nexus 7)中看起來很小。我們可以通過xml來實現嗎? 或者,我將不得不爲相同的圖像在drawable-hdpi,drawable-xhdpi等中存儲不同的圖像大小。我有近50張圖片。保存多個副本將會增加我的應用程序的大小了很多:(在較大的屏幕(Nexus 7)上圖像看起來微乎其微,但在較小的屏幕(Nexus 4)中填充整個屏幕

回答

1

Is there a way to set imageview size as a proportion of display size?

使用LinearLayoutandroid:layout_weight

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="50" 
     android:text="@string/fifty_percent"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="30" 
     android:text="@string/thirty_percent"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="20" 
     android:text="@string/twenty_percent"/> 

</LinearLayout> 

(從this sample project

或者,使用PercentFrameLayout

<?xml version="1.0" encoding="utf-8"?> 
<android.support.percent.PercentFrameLayout 
    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"> 

    <!--suppress AndroidDomInspection --> 
    <TextView 
    android:id="@+id/start" 
    android:layout_height="wrap_content" 
    android:background="#FF00FFFF" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textColor="@android:color/black" 
    app:layout_marginLeftPercent="5%" 
    app:layout_widthPercent="30%"/> 

    <!--suppress AndroidDomInspection --> 
    <TextView 
    android:id="@+id/center" 
    android:layout_height="wrap_content" 
    android:background="#FFFF00FF" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textColor="@android:color/black" 
    app:layout_marginLeftPercent="40%" 
    app:layout_widthPercent="20%"/> 

    <!--suppress AndroidDomInspection --> 
    <TextView 
    android:id="@+id/end" 
    android:layout_height="wrap_content" 
    android:background="#FFFFFF00" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textColor="@android:color/black" 
    app:layout_marginLeftPercent="65%" 
    app:layout_widthPercent="30%"/> 
</android.support.percent.PercentFrameLayout> 

或者,使用PercentRelativeLayout

<?xml version="1.0" encoding="utf-8"?> 
<android.support.percent.PercentRelativeLayout 
    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"> 

    <!--suppress AndroidDomInspection --> 
    <TextView 
    android:id="@+id/start" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:background="#FF00FFFF" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textColor="@android:color/black" 
    app:layout_marginLeftPercent="5%" 
    app:layout_widthPercent="30%"/> 

    <!--suppress AndroidDomInspection --> 
    <TextView 
    android:id="@+id/center" 
    android:layout_height="wrap_content" 
    android:layout_toEndOf="@id/start" 
    android:background="#FFFF00FF" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textColor="@android:color/black" 
    app:layout_marginLeftPercent="5%" 
    app:layout_widthPercent="20%"/> 

    <!--suppress AndroidDomInspection --> 
    <TextView 
    android:id="@+id/end" 
    android:layout_height="wrap_content" 
    android:layout_toEndOf="@id/center" 
    android:background="#FFFFFF00" 
    android:gravity="center" 
    android:padding="8dp" 
    android:textColor="@android:color/black" 
    app:layout_marginLeftPercent="5%" 
    app:layout_widthPercent="30%"/> 
</android.support.percent.PercentRelativeLayout> 

(那些後兩者是從this sample app

​​

屏幕密度(HDPI,MDPI等)是不相關的篩選大小

+0

謝謝!愚蠢的我把屏幕大小與屏幕密度聯繫起來..我嘗試使用PercentRelativeLayout,並且出現錯誤錯誤:(7)沒有找到屬性'layout_widthPercent'的資源標識符com.myPackage.myClass – user5673235

+0

LinearLayout的工作原理。謝謝 :) – user5673235