图片没有对齐顶部?

时间:2015-12-29 12:12:15

标签: android xml android-layout

所以在我的程序中我实现了一个布局,其中我希望图像与父级的alignTop。我曾经为另一个活动(略有变化)完全相同的布局代码,它完美地工作,但在此活动中使用它时却没有。我的图像仍然与视图的中心对齐。这是布局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>

1 个答案:

答案 0 :(得分: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" />