0

Я показываю полноэкранное изображение в ImageView, и я пытаюсь добавить TextView в центр экрана поверх ImageView, но TextView не отображается.Добавить TextView над ImageView (Android)

я пытался:

<?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> 
+2

использование RelativeLayout или FrameLayout вместо Linear – Suvitruf

+0

http://stackoverflow.com/questions/19065232/placing-a-textview-on-top-of-imageview-in-android –

ответ

2

вы можете использовать FrameLayout, AbsoluteLayout (устаревший) или 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> 

чертеж компоновки в порядке, как и в XML, так что ImageView будет обращено сначала и затем TextView

1

<FrameLayout ...> привлекает его детей один на другой.

Обратите внимание, что вы можете указать фон как полностью прозрачный.

0

Try This

<?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> 
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> 
+0

То, что я хотел, намного лучше, чем FrameLayout –

Смежные вопросы