如何在一个 LinearLayout 内的多个图像上设置文本?

How to set text over multiple images which is inside one LinearLayout?

我想在图片上加一些文字。有人可以帮我解决这个问题吗?

我有六个矩形框,我想在每个矩形框上放一些文字。我用 ImageView 来制作矩形。

请找到以下mainactivity.xml代码:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.vimal.edkul.StudentProfile">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout1">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/title">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:src="@drawable/pic13" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="User Profile"
                android:textSize="20sp"
                android:textColor="@android:color/white" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:src="@drawable/ic_action_overflow" />
        </android.support.v7.widget.Toolbar>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout2"
        android:layout_gravity="center"
        android:layout_below="@+id/layout1"
        android:padding="30dp">
        <FrameLayout
            android:layout_height="100dp"
            android:layout_width="130dp">
            <View
                android:background="@drawable/rectangle"
                android:clickable="false"
                android:id="@+id/view1"
                android:layout_height="match_parent"
                android:layout_width="match_parent" />
            <TextView
                android:gravity="center"
                android:id="@+id/text1"
                android:textColor="#000"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:text="Some text" />
        </FrameLayout>
        <FrameLayout
            android:layout_height="100dp"
            android:layout_width="130dp">
            <View
                android:background="@drawable/rectangle"
                android:clickable="false"
                android:id="@+id/view2"
                android:layout_height="match_parent"
                android:layout_width="match_parent" />
            <TextView
                android:gravity="center"
                android:id="@+id/text2"
                android:textColor="#000"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:text="Some text" />
        </FrameLayout>
    </LinearLayout>
</RelativeLayout>

rectangle.xml

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
    <solid android:color="@android:color/transparent"/>
    <corners android:radius="12px"/>
    <stroke  android:width="2dip" android:color="#000000"/>
</shape>

您可以使用以下布局部分代替每个 ImageView:

<FrameLayout
    android:layout_height="150dp"
    android:layout_width="100dp" >

    <View
        android:background="@drawable/rectangle"
        android:clickable="false"
        android:id="@+id/buttonVote"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

    <TextView
        android:gravity="center"
        android:id="@+id/textStatusMessage"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="Some text" />

</FrameLayout>

编辑

您可以在 rectangle.xml 中更改背景。例如:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/darker_gray" />
    <corners android:radius="12px" />
    <stroke
        android:width="2dip"
        android:color="#000000" />
</shape>

对我来说 xml looks that

您甚至可以删除 FrameLayoutView 并为 TextView 设置背景:

<TextView
        android:id="@+id/text1"
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="Some text"
        android:textColor="#000"
        android:background="@drawable/rectangle"/>

P.S。要显示简单的图像,您不需要使用 ImageView,简单的 View 和背景就足够了。