如何在imageview中添加标题?

时间:2017-05-17 15:45:13

标签: android android-studio android-fragments

我有这个: enter image description here

但我想: enter image description here

我的XML:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="380dp"
    android:layout_margin="0dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true">

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

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:paddingBottom="-10dp"
            android:scaleType="fitCenter"
            android:src="@drawable/dog_full"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            android:padding="8dp"
            android:textColor="#222"
            android:textSize="22dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:padding="8dp"
            android:textColor="#222"
            android:textSize="18dp"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

我怎么做到这一点?谢谢!

你能告诉我们如何制作这个标记吗?你能告诉我们如何制作这个标记吗?

3 个答案:

答案 0 :(得分:1)

由于您的标题已经在RelativeLayout中并且它位于缩略图之下,因此您可以在标题中添加负marginTop。类似的东西:

   <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:padding="8dp"
        android:marginTop="-30dp"
        android:textColor="#222"
        android:textSize="22dp"
        android:textStyle="bold" />

您可以根据需要调整dp。

答案 1 :(得分:1)

只需使用android:layout_alignBottom

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardUseCompatPadding="true"
    android:layout_marginLeft="@dimen/card_margin"
    android:layout_marginRight="@dimen/card_margin">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img_card"
            android:layout_width="match_parent"
            android:layout_height="@dimen/cafe_img"/>
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size_large_plus"
            android:layout_alignBottom="@id/img_card"
            android:textColor="@color/colorBackground"
            android:background="@color/transparentColor"/>

    </RelativeLayout>

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

答案 2 :(得分:0)