Android:ImageView底部带有TextView

Cod*_*oga 0 android android-layout

我试图在图像下面放置一个带有标签的图像,我一直在尝试很多但是无法阻止图像和文本重叠,这是我的layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"     >

<ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="15dp"
        android:paddingLeft="15dp"/>


<TextView
        android:id="@+id/grid_item_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:singleLine="true"
        android:layout_marginLeft="20dp"
        android:paddingLeft="15dp"
        android:textSize="20dp">
</TextView>
Run Code Online (Sandbox Code Playgroud)

Gui*_*ian 6

您可以使用textview的复合drawable来简化布局.

删除图像视图并将drawableTop设置为文本视图,如下所示:

<TextView  
    android:id="@+id/grid_item_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    ...
    android:text="My Text" 
    android:drawableTop="@drawable/my_drawable"/>
Run Code Online (Sandbox Code Playgroud)

您还可以使用setCompoundDrawable方法在java代码中设置drawable

有关此主题的更多信息,请参阅此博文.