在android中的ImageView上显示TextView

我想在图像视图上显示文本。 我就像Alesqui在这里建议的那样: Android Text over image

Android studio中的预览看起来很好: android Studio预览xml

但是我的实际结果与上面的文字不一样:

模拟器中的代码

我必须在执行期间动态地将以下XML添加到LinearLayout:

    

我按以下方式添加它:

 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有关。 获得结果的正确方法是什么?

试试这个:

     

提示:

  • Romain的家伙说:“我没有说不使用RelativeLayout,你只需要知道它(如果在树顶部使用)对性能的影响。” 如果可以,使用简单的FrameLayout要好得多。

  • 请注意ImageView中的scaleType属性

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

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

  • 我更喜欢Picasso用于位图管理。