java 在android中在ImageView上显示TextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26976135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:59:05  来源:igfitidea点击:

Display TextView over ImageView in android

javaandroidandroid-layouttextviewimageview

提问by dba

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 preview of the xml

Android Studio 中的预览看起来不错: xml的android Studio预览

But my actual result looks like this with the text unwantedly above:

但是我的实际结果看起来像上面不需要的文本:

Code in Emulator

模拟器中的代码

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. What is the proper way to do get the result?

这一定与那个父 LinearLayout 有关。获得结果的正确方法是什么?

采纳答案by Karakuri

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>

回答by jpardogo

Tips:

尖端:

  • Romain guysaid: "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." Much better to use a simple FrameLayout if you can.

  • Be aware of the scaleTypeproperty in ImageView's

  • 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. If you use wrap content it will depend on the size of the bitmap and the resolution.

  • Consider using match_parent instead of the deprecatedfill_parent

  • I would prefer Picassofor bitmaps management.

  • Romain 家伙说:“我没有说不要使用 RelativeLayout,你只是必须意识到如果在树的顶部使用它对性能的(可能的)影响。” 如果可以,最好使用简单的 FrameLayout。

  • 注意ImageView 中的scaleType属性

  • 如果您希望 ImageView 与父级的大小相同,则需要将其设置为 match_parent 的高度和宽度。如果您使用换行内容,它将取决于位图的大小和分辨率。

  • 考虑使用 match_parent 而不是已弃用的fill_parent

  • 我更喜欢毕加索的位图管理。

-

——

<?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>