首页 > 解决方案 > 将 TextView 放在具有高度的图像视图之上

问题描述

我已经对堆栈溢出进行了广泛的研究,但没有找到适合我的答案。我所拥有的是ImageView一个elevation(用于阴影效果)在我想要一个TextView. 我将对 a 中的许多元素重复此操作GridLayout,因此bringToFront()看起来不是一个好的选择。此外,我不希望TextView有阴影,因此 的translationZ属性xml也不起作用。最后,我尝试将 aFrameLayout用于这两个元素,但我认为由于 上的海拔属性ImageviewTextView仍然保留在后面。以下是我的代码

<android.support.v7.widget.CardView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_columnWeight="1"
    android:layout_rowWeight="2"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    app:cardElevation="8dp"
    app:cardCornerRadius="8dp">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_gravity="center">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:gravity="center">
            <TextView
                android:id="@+id/bids_number"
                android:background="@drawable/circulerimageblue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10"
                android:layout_marginBottom="-7dp"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:paddingTop="1dp"
                android:paddingBottom="1dp"
                android:textStyle="normal|bold"
                android:textColor="@color/colorSecondary"
                android:layout_gravity="top|right" />
            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:elevation="6dp"
                android:id="@+id/bids_icon"
                android:background="@drawable/circulerimageunselected"
                android:layout_marginBottom="6dp"
                android:src="@drawable/icon_profile"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Get Bids"
                android:textAppearance="@style/TextAppearance.AppCompat"
                android:textStyle="normal|bold"
                android:fontFamily="sans-serif" />
        </LinearLayout>

    </RelativeLayout>

</android.support.v7.widget.CardView>

这是我想要达到的目标的图片

在此处输入图像描述

标签: androidandroid-layout

解决方案


注意:请不要使用负边距

试试这个使用CoordinatorLayout

<android.support.v7.widget.CardView 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="wrap_content"
    android:layout_column="0"
    android:layout_columnWeight="1"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:layout_row="0"
    android:layout_rowWeight="2"
    app:cardCornerRadius="8dp"
    app:cardElevation="8dp">


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:id="@+id/topPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/bids_icon"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="6dp"
                android:adjustViewBounds="true"
                android:elevation="6dp"
                android:scaleType="fitXY"
                android:src="@drawable/kid" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Get Bids"
                android:textAppearance="@style/TextAppearance.AppCompat"
                android:textStyle="normal|bold" />

        </LinearLayout>

        <TextView
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPink"
            android:text="10"
            android:textStyle="normal|bold"
            app:layout_anchor="@+id/bids_icon"
            app:layout_anchorGravity="right" />


    </android.support.design.widget.CoordinatorLayout>

</android.support.v7.widget.CardView>

输出

在此处输入图像描述


推荐阅读