How can I add a TextView into an ImageView in GridView Layout?

这一生的挚爱 提交于 2019-12-02 02:08:48

Create a RelativeLayout that contains the ImageView and the TextView,with TextView's bottom edge aligned with the Imageview's bottom edge.

<RelativeLayout ...>
    <ImageView ...>
    <TextView ...
      android:layout_alignBottom="id of the imageview"> // or to top
</RelativeLayout>

Inflate this layout in getView method of your adapter.

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

    <ImageView
        android:id="@+id/grid_outer_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/image_border" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <TextView
            android:id="@+id/grid_inner_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text" />
    </RelativeLayout>

</RelativeLayout>

Just to extend userSeven7s' answer, you should return the RelativeLayout instead of ImageView, so that the GridView have RelativeLayout as it's direct child, instead of ImageView and RelativeLayout can have TextView and ImageView etc.
You can get the object of RelativeLayout by the following method:

RelativeLayout rel = (RelativeLayout)View.inflate(R.layout.YOUR_RELATIVE_LAYOUT_XML_HERE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!