简体   繁体   English

在android中的ImageView上显示TextView

[英]Display TextView over ImageView in android

I want to display a text over an Image View. 我想在图像视图上显示文本。 I do it like Alesqui suggested here: Android Text over image 我就像Alesqui在这里建议的那样: Android Text over image

The preview in Android studio looks fine: Android studio中的预览看起来很好: android Studio预览xml

But my actual result looks like this with the text unwantedly above: 但是我的实际结果看起来像上面的文字:

模拟器中的代码

I have to add the following XML dynamically to a LinearLayout during the execution: 我必须在执行期间动态地将以下XML添加到LinearLayout:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <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" />

</RelativeLayout>

I add it the following way: 我按以下方式添加它:

LinearLayout ll = (LinearLayout) findViewById(R.id.layout_story_covers);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    for (int i = 0; i < stories.size(); i++)
    {
        // add  imageView
        RelativeLayout coverLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_cover, null);
        ImageView imageView =(ImageView) coverLayout.findViewById(R.id.imageViewCover);
        TextView textView = (TextView) coverLayout.findViewById(R.id.textViewCover);
        textView.setText(stories.get(i).title);
        imageLoader.displayImage(stories.get(i).cover.fileUrl, imageView);
        ll.addView(coverLayout);
    }

This must have something to do with that parent LinearLayout. 这必须与父级LinearLayout有关。 What is the proper way to do get the result? 获得结果的正确方法是什么?

Try this instead: 试试这个:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <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_centerInParent="true"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />

</RelativeLayout>

Tips: 提示:

  • Romain guy said: "I didn't say don't use RelativeLayout, you just must be aware of its (possible) impact on performance if used at the top of the tree." Romain的家伙说:“我没有说不使用RelativeLayout,你只需要知道它(如果在树顶部使用)对性能的影响。” Much better to use a simple FrameLayout if you can. 如果可以,使用简单的FrameLayout要好得多。

  • Be aware of the scaleType property in ImageView's 请注意ImageView中的scaleType属性

  • If you want the ImageView to be the same size as the parent then you need to set it as match_parent height and width. 如果您希望ImageView与父级的大小相同,则需要将其设置为match_parent的高度和宽度。 If you use wrap content it will depend on the size of the bitmap and the resolution. 如果使用换行内容,则取决于位图的大小和分辨率。

  • Consider using match_parent instead of the deprecated fill_parent 考虑使用match_parent而不是弃用的 fill_parent

  • I would prefer Picasso for bitmaps management. 我更喜欢Picasso用于位图管理。

- -

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

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

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/spacing_medium"
        android:layout_gravity="center"
        tools:text="Test"/>
</FrameLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM