2014-05-24 95 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