在ImageView上添加TextView(Android)

时间:2014-12-11 14:19:09

标签: android android-layout android-imageview textview

我在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>

6 个答案:

答案 0 :(得分:8)

您可以使用FrameLayoutAbsoluteLayout(已弃用)或RelativeLayout(最常见的)。 RelativeLayout的示例,用于居中android:layout_centerInParent="true"

<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" />

</RelativeLayour>

绘制view与XML中的顺序相同,因此首先绘制ImageView,然后TextView,但记住android:elevation xml属性和{{{ 1}} Java方法

答案 1 :(得分:2)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
        android:layout_height="match_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:layout_centerInParent="true"
        android:padding="12dip"
        android:text="SomeText"
        android:textColor="#ffffffff" />

</RelativeLayout>

答案 2 :(得分:1)

<FrameLayout ...>将孩子们拉到另一个上面。

请注意,您可以将背景指定为完全透明。

答案 3 :(得分:0)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/myImage" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am TextView"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

答案 4 :(得分:0)

有一个新属性 android:elevation 可用于解决此问题。 但请注意,该API在API 21或更高版本中使用。

 <TextView
    android:id="@+id/welcome_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:elevation="100dp"
    />

答案 5 :(得分:0)

在相对布局中,您还可以在 TextView 中使用以下属性,使其显示在 ImageView 之上:

android:layout_alignLeft="@+id/imageView1" 
android:layout_alignRight="@+id/imageView1"
相关问题