2015-12-29 152 views
-1

所以在我的程序中我實現了一個佈局,我希望圖像能夠對齊父窗口。我曾經爲另一個活動確定了相同的佈局代碼(只有很小的變化),並且它完美地工作,但是在此活動中使用它時卻沒有。我的形象仍然與視野中心保持一致。這裏是佈局的xml圖片不對齊頂部?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/darkGray" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.intellidev.fuzionvapor.HomeDetails" 
    tools:showIn="@layout/activity_home_details"> 

    <ImageView 
     android:id="@+id/homeDetailsActivityImage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:scaleType="fitCenter" /> 

    <TextView 
     android:id="@+id/homeDetailsActivityTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/homeDetailsActivityImage" 
     android:textColor="@color/white" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/homeDetailsActivityPrice" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/homeDetailsActivityImage" 
     android:textColor="@color/fuzionRed" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <View 
     android:id="@+id/homeDetailsActivityRuler" 
     android:layout_width="fill_parent" 
     android:layout_height="1dip" 
     android:layout_below="@+id/homeDetailsActivityTitle" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:background="@color/white" /> 

    <TextView 
     android:id="@+id/homeDetailsActivityDescription" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/homeDetailsActivityRuler" 
     android:textColor="@color/white" 
     android:textSize="15sp" /> 

    <LinearLayout 
     android:id="@+id/homeDetailsFooter" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/homeDetailsActivityButton" 
     android:layout_centerHorizontal="true" 
     android:orientation="horizontal"> 

     <fr.ganfra.materialspinner.MaterialSpinner 
      android:id="@+id/homeDetailsActivitySizeSpinner" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      app:ms_arrowColor="@color/fuzionRed" 
      app:ms_arrowSize="16dp" 
      app:ms_baseColor="@color/fuzionRed" 
      app:ms_enableFloatingLabel="true" 
      app:ms_floatingLabelColor="@color/fuzionRed" 
      app:ms_floatingLabelText="@string/sizeFloatingLabel" 
      app:ms_highlightColor="@color/fuzionRed" 
      app:ms_hintColor="@color/fuzionRed" 
      app:ms_hint="Size" 
      app:ms_multiline="false" 
      app:ms_thickness="1dp" /> 

     <fr.ganfra.materialspinner.MaterialSpinner 
      android:id="@+id/homeDetailsActivityNicotineSpinner" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="1dp" 
      android:layout_weight="1" 
      app:ms_arrowColor="@color/fuzionRed" 
      app:ms_arrowSize="16dp" 
      app:ms_baseColor="@color/fuzionRed" 
      app:ms_enableFloatingLabel="true" 
      app:ms_floatingLabelColor="@color/fuzionRed" 
      app:ms_floatingLabelText="@string/nicotineFloatingLabel" 
      app:ms_highlightColor="@color/fuzionRed" 
      app:ms_hintColor="@color/fuzionRed" 
      app:ms_multiline="false" 
      app:ms_hint="Nicotine" 
      app:ms_thickness="1dp" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/homeDetailsActivityButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:background="@color/fuzionRed" 
     android:text="Add To Cart" 
     android:textAlignment="center" 
     android:textColor="@color/white" 
     android:textSize="25sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

回答

2

變化

<ImageView 
     android:id="@+id/homeDetailsActivityImage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:scaleType="fitCenter" /> 

<ImageView 
     android:id="@+id/homeDetailsActivityImage" 
     android:layout_width="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitStart" /> 
+0

這樣做。清理並運行構建,但仍然無法運行 –

+0

將android:scaleType =「fitCenter」更改爲android:scaleType =「fitStart」 –

+0

太棒了。工作 –