2015-01-03 38 views
0

我想创建一个有图像视图的布局和另一个包含2个textview的LinearLayout。我希望imageView占据总布局高度的60%,并通过textView的布局获取剩余空间。但是,问题是imageview的高度不会占总高度的60%。它随图像的尺寸而变化。我一直无法弄清楚这种行为背后的原因。了解Layout_Weight的线性布局

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myView” 
    android:layout_width="@dimen/my_width" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="6dp" 
    android:orientation="vertical" 
    > 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="1.0" 
     android:orientation="vertical" > 


      <ImageView 
       android:id="@+id/thumbnail" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_alignParentTop="true" 
       android:scaleType="centerCrop" 
       android:layout_weight=“0.6” 
       android:src="@drawable/ic_launcher"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.40” 
      android:layout_below="@+id/imageLayout" 
      android:orientation="vertical" 
      > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       > 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_marginTop="7dp" 
       android:maxLines="1" 
       android:paddingLeft="@dimen/my_padding" 
       android:paddingRight="@dimen/my_padding" 
       android:text=“hello “world/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:layout_marginTop="20dp" 
       android:paddingLeft="16dp" 
       android:text=“ty”/> 


       <View 
        android:layout_width="0dp" 
        android:layout_height=“match_parent" 
        android:layout_weight="1" 
        ></View> 
       <Button 
        android:layout_width="@dimen/but_width" 
        android:layout_height="@dimen/but_height" 
        android:layout_gravity="right|bottom" 
        android:text=“button” 

        /> 

       </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 

    </FrameLayout> 

此致

的观点

回答

0

宽度和高度不只是从layout_weight属性依赖。例如,它可以取决于它的孩子的大小(如果它是一个视图组)。您必须始终避免使用嵌套布局,因为它使用更多的内存。而不是框架布局和几个线性布局,您可以使用一个相对布局。