dba*_*dba 6 java android textview imageview android-layout
我想在图像视图上显示文本。我按照 Alesqui 的建议这样做: Android Text over image
Android studio 中的预览看起来不错:
但我的实际结果看起来像这样,上面有不必要的文本:
我必须在执行期间将以下 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>
Run Code Online (Sandbox Code Playgroud)
我通过以下方式添加它:
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);
}
Run Code Online (Sandbox Code Playgroud)
这一定与父 LinearLayout 有关。获得结果的正确方法是什么?
试试这个:
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17558 次 |
最近记录: |