在Android中在ImageView上显示TextView

6
我希望在图片视图上显示文本。我按照Alesqui在这里提出的建议来实现: Android图片上的文本 在Android Studio中预览效果看起来不错: android Studio preview of the xml 但是,我的实际结果是这样的,文本不必要地位于上方: Code in Emulator 我必须在执行期间动态向LinearLayout添加以下XML:
<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);
    }

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


文字颜色不对,应该是黑色而不是黄色。并且应该显示“Hello”,而不是“My Text”。 - njzk2
是的,它不一致。但由于其较低的重要性,我忽略了它。 - dba
2个回答

5
尝试使用以下内容替代:
<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>

这个答案没有提供许多东西,而且你应该停止这样做。我也有一个答案,试图指出它们。它不应该被选择为正确的答案。如果有人对我的答案有更多改进,请在评论中提出。 - jpardogo
我也更喜欢使用FrameLayout,但我不知道他的应用程序的完整上下文,可能他想做其他需要RelativeLayout的事情(也许他会添加更多视图?),因此我有意只提供解决他即时问题的更改。 - Karakuri

2

提示:

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

  • 注意ImageView的scaleType属性

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

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

  • 对于位图管理,我更喜欢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>

基于这篇文章 https://www.infinum.co/the-capsized-eight/articles/top-5-android-libraries-every-android-developer-should-know-about,我选择了UIL库。[这也很有趣](https://dev59.com/2WIj5IYBdhLWcg3wsm-n)。 - dba
好的,我应该修改我的回答并说Picasso只是我的个人喜好。 - jpardogo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接