我在 ImageView 中显示全屏图像,我试图在 ImageView 的屏幕中心添加 TextView,但 TextView 没有显示。
我试过:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/splash_imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="SomeText"
android:textColor="#ffffffff" />
</LinearLayout>
最佳答案
您可以使用 FrameLayout
、AbsoluteLayout
(已弃用)或 RelativeLayout
(其中最常见)。 RelativeLayout
的示例,用于居中 android:layout_centerInParent="true"
,为两个 child 设置
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/splash_imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dip"
android:layout_centerInParent="true"
android:background="#AA000000"
android:padding="12dip"
android:text="SomeText"
android:textColor="#ffffffff" />
</RelativeLayout>
绘制 View
的顺序与在 XML 中的顺序相同,因此将首先绘制 ImageView
,然后在其上绘制 TextView
另一种方法是使用 FrameLayout
和 android:gravity="center"
attr( child 不需要)
还要记住并阅读有关 android:elevation
和 android:translationZ
的 XML 属性,它们可能会重新排序绘图,还有 view.bringToFront()
方法
关于android - 在 ImageView 上添加 TextView (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425026/