2014-05-24 93 views
0

我想問如何設置imageview的procentual寬度和高度?Android設置procentual大小imageView

我試圖找到任何工作解決方案,但沒有形成他們ws不爲我工作。

感謝您的任何幫助。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:id="@+id/layoutContainer" android:orientation="vertical"> 

    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3"> 
     <!--HERE I NEED TO SET PROCENTUAL HEIGHT --> 
     <ImageView 
     android:id="@+id/ivFlag1" 
     android:layout_alignParentTop="true" 
     android:layout_width="200dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:src="@drawable/car_finder_logo" /> 

    </RelativeLayout> 
</LinearLayout> 
+0

爲什麼你裏面有一個線性相對佈局? – pskink

+0

您在'ImageView'中定義了兩次layout_width。這甚至不會編譯。 – joao2fast4u

回答

0

android:layout_weight將導致消耗母體的高度/寬度的觀點,如果它是父的唯一子。將dummy view添加到LinearLayout並根據您需要的百分比設置其android:layout_weight

假設你需要的ImageView採取母公司的50%,修改佈局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:id="@+id/layoutContainer" android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="3"> 

     <ImageView 
      android:id="@+id/ivFlag1" 
      android:layout_alignParentTop="true" 
      android:layout_width="fill_parent" 
      android:layout_height="fil_parent" 
      android:layout_marginTop="15dp" 
      android:src="@drawable/car_finder_logo" /> 

     </RelativeLayout> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" /> 

</LinearLayout> 
0

剛剛成立,其中LinearLayout,默認情況下,孩子的權重之和爲1 您的LinearLayout(即rootView)將有一個ImageView孩子。要將其高度設置爲百分比,只需使用0dp的layout_height和0.3的layout_weight爲30%或0.5爲50%。就像這樣:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layoutContainer" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:weightSum="1" > 

<!-- HERE I NEED TO SET PROCENTUAL HEIGHT --> 

<ImageView 
    android:id="@+id/ivFlag1" 
    android:layout_width="200dp" 
    android:layout_height="0dp" 
    android:layout_weight="0.25" 
    android:layout_gravity="center" 
    android:src="@drawable/ic_launcher" /> 

的結果是具有版面高度的25%,你的ImageViewThe result