Imageview未在父级中居中-Android

Imageview未在父级中居中-Android,android,imageview,Android,Imageview,我试图将蓝色图标精确定位在卡的中心,但由于某种原因,图标会上升到卡的顶部。 有人能告诉我怎么了吗? 谢谢 以下是我的问题的截图: 我假设在gridview项中,您的父项是LinearLayout,从代码片段中看不到它。如果是这种情况,请在FrameLayout属性中设置包裹内容的高度 <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout

我试图将蓝色图标精确定位在卡的中心,但由于某种原因,图标会上升到卡的顶部。 有人能告诉我怎么了吗? 谢谢

以下是我的问题的截图:




我假设在gridview项中,您的父项是LinearLayout,从代码片段中看不到它。如果是这种情况,请在FrameLayout属性中设置包裹内容的高度

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:focusable="true"
android:foreground="?android:selectableItemBackground"
android:background="@drawable/document_grid_item_bg">

给定的代码片段与
LinearLayout
作为父级可以正常工作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"
        android:focusable="true"
        android:background="@color/colorGreen"
        android:foreground="?android:selectableItemBackground">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/draft_more_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="5dp"
                app:srcCompat="@drawable/ic_more" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/draft_icon"
                android:layout_centerInParent="true" />

        </RelativeLayout>
    </FrameLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="0.1">

        <TextView
            android:id="@+id/draft_date_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Date" />
    </RelativeLayout>

</LinearLayout>

1.删除
FrameLayout
并将其
属性添加到child
RelativeLayout

2.使用
属性android:src
而不是
app:srccomat
ImageView
上设置
image

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"
        android:background="#fff">

        <ImageView
            android:id="@+id/draft_more_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/ic_more" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/draft_icon" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="0.1">

        <TextView
            android:id="@+id/draft_date_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="16 May 2017" />
    </RelativeLayout>

</LinearLayout>

FYI,如果您使用
CardView
作为容器,则只需将
LinearLayout
放在
CardView
的下方即可

输出:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"
        android:background="#fff">

        <ImageView
            android:id="@+id/draft_more_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/ic_more" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/draft_icon" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="0.1">

        <TextView
            android:id="@+id/draft_date_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="16 May 2017" />
    </RelativeLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"
        android:background="#fff">

        <ImageView
            android:id="@+id/draft_more_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/ic_more" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/draft_icon" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="0.1">

        <TextView
            android:id="@+id/draft_date_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="16 May 2017" />
    </RelativeLayout>

</LinearLayout>