在android中的imageview之上放置一个textview [英] Placing a textview on top of imageview in android
问题描述
- 我有一个
listview
,它有一个imageview
,它是可垂直滚动 - 我正在尝试在
Imageview
之上放置一个 - 两个视图都必须可见
textview
- I have a
listview
, that has a singleimageview
which is scrollable vertically - I am trying to place a
textview
on top ofImageview
- Both the views must be visible
- 有可能吗?
- 如果是,如何以编程方式进行?
- 我需要做哪些改变?
<小时>
list_view_item_for_images.xml
list_view_item_for_images.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
它给出如下输出
如何做类似下面的事情
注意 :: 菜 1 &2 是文本视图
note :: Dish 1 & 2 are textviews
推荐答案
这应该给你所需的布局:
This should give you the required layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
使用 android:layout_marginTop="20dp"
看看哪个更适合您.使用id textview
动态设置android:text
值.
Play with the android:layout_marginTop="20dp"
to see which one suits you better. Use the id textview
to dynamically set the android:text
value.
由于 RelativeLayout 堆叠其子项,因此在 ImageView 将其置于"ImageView 之上之后定义 TextView.
Since a RelativeLayout stacks its children, defining the TextView after ImageView puts it 'over' the ImageView.
注意: 使用 FrameLayout
作为父级可以获得类似的结果,并且比使用任何其他 android 容器效率更高.感谢 Igor Ganapolsky
(见下面的评论)指出这个答案需要更新.
NOTE: Similar results can be obtained using a FrameLayout
as the parent, along with the efficiency gain over using any other android container. Thanks to Igor Ganapolsky
(see comment below) for pointing out that this answer needs an update.
这篇关于在android中的imageview之上放置一个textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!