как установить текст над изображением?

Я размещаю панель приложений в верхней части экрана, и она выглядит следующим образом:

введите здесь описание изображения

Теперь я хочу установить текст поверх изображения, чтобы изображение выглядело следующим образом:

введите здесь описание изображения

Как установить текст поверх изображения? заранее спасибо


person Siva K    schedule 25.04.2011    source источник


Ответы (8)


Пожалуйста, попробуйте приведенный ниже код.

<?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@drawable/your background image">


            <LinearLayout 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent" 
                android:layout_weight="1"
                android:gravity="center" 
                android:orientation="vertical">

                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" 
                    android:textColor="@color/white"
                    android:textSize="18sp" 
                    android:textStyle="bold"
                    android:singleLine="true" 
                    android:text="Subject"/>

            </LinearLayout>

</LinearLayout>
person Chirag    schedule 25.04.2011
comment
Это действительно должен быть TextView с набором рисунков в качестве фона. - person AllDayAmazing; 18.08.2016

Если вы просто хотите, чтобы текст располагался по центру изображения, все, что вам нужно, это TextView, а не изображение. Установите android:background="@drawable/yourImage" в TextView.

person Tenfour04    schedule 25.04.2011
comment
Но это работает только для статических изображений. Что, если, например, новостное приложение хочет установить заголовок поверх изображения? Изображения и изменения для каждой новости - person Ojonugwa Jude Ochalifu; 20.08.2015
comment
@ojonugwaochalifu textView.setBackgroundDrawable() и textView.setText() - person Tenfour04; 10.09.2015

Вот как я это сделал, и это сработало именно так, как вы просили:

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myImageSouce" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

Используйте относительный макет для этого

person Rita    schedule 15.01.2013
comment
Как я часто обнаруживал, многие из них больше не работают. В этом случае мне пришлось изменить android:gravity на android:layout_gravity="center" - person SpacemanScott; 03.12.2015

  1. Используйте FrameLayout (учебник)
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/ImageView01"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:src="@drawable/lake"
        android:scaleType="matrix"></ImageView>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="40dp"
        android:text="@string/top_text" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/bottom_text"
        android:layout_gravity="bottom"
        android:gravity="right"
        android:textColor="#fff"
        android:textSize="50dp" />
</FrameLayout>

Or

2. Используйте изображение в качестве фона: android:background="@drawable/yourImage"

person Ajay Patel    schedule 25.04.2011

есть много способов отображать текст поверх изображения

1) вы можете сделать это изображение с текстом.. 2) вы можете установить фон (это изображение) для просмотра текста.

person Niranj Patel    schedule 25.04.2011

Если вы хотите поместить какое-либо представление в любое другое представление в относительном макете, вам необходимо определить представление верхнего уровня ниже представления низкого уровня... например

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myImageSouce" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />
person V Jayakar Edidiah    schedule 23.12.2014

Если я правильно догадался, вы пытаетесь использовать заголовок для своего образца.

Установите фон как это изображение и добавьте текстовое представление к нему, как это.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="55px"
    android:background="@drawable/testheader"
    android:layout_width="fill_parent"
    android:orientation="horizontal">           
        <TextView android:id="@+id/quaddeal_header_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dip"
            android:text="Campus"
            android:textSize="20sp"
            android:textColor="@color/white"
            android:layout_marginLeft="90dip"/>
</RelativeLayout>

Или вам нужно объединить макеты с помощью Framelayout.

Посмотрите этот пример макета фрейма.

person Venky    schedule 25.04.2011

Вы можете использовать FrameLayout для получения текста поверх изображения, как показано ниже:

<FrameLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton
        android:id="@+id/imgbtnUploadPendingPods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/com" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_gravity="center"
        android:text="hii"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
person pm dubey    schedule 15.01.2017