2013-09-28 19 views
40
  • Ho un listview, che ha un unico imageview che è scorrevole verticalmente
  • Sto cercando di mettere un textview in cima Imageview
  • Entrambi i punti di vista devono essere visibile

  1. E 'possibile?
  2. Se sì, come si fa a livello di programmazione?
  3. Quali modifiche dovrei apportare?

list_view_item_for_images.xmlEsecuzione di una TextView in cima ImageView in Android

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

dà un output come sotto

enter image description here

Come fare qualcosa di simile sotto

enter image description here

nota :: Dish 1 & 2 sono textviews

+0

aggiungi la vista del testo sopra ImageView –

+0

puoi mostrarlo modificando xml come risposta .... textview deve essere visibile in cima alla visualizzazione dell'immagine –

+0

il segreto è ** RelativeLayout **. (Piuttosto che LinearLayout, ecc.) – Fattie

risposta

73

Questo dovrebbe dare il layout desiderato:

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

Play with android:layout_marginTop="20dp" per vedere quale vi si addice meglio. Utilizzare l'ID textview per impostare dinamicamente il valore .

Poiché un RealativeLayout impila i suoi figli, definendo il TextView dopo ImageView lo mette sopra 'ImageView.

NOTA: Risultati simili possono essere ottenuti utilizzando uno FrameLayout come genitore, insieme con il guadagno di efficienza rispetto all'utilizzo di qualsiasi altro contenitore Android. Grazie a Igor Ganapolsky (vedi commento sotto) per sottolineare che questa risposta ha bisogno di un aggiornamento.

+1

Risposta molto informativa ...... grazie! ..... e funziona –

+0

@Sky Sei il benvenuto. – Vikram

+1

Se si desidera allineare '' verticalmente, rimuovere' android: layout_alignParentTop', 'android: layout_marginTop' e aggiungere' android: layout_centerInParent = "true" 'a' '. –

8

Prova questo:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rel_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/ImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src=//source of image /> 

<TextView 
    android:id="@+id/ImageViewText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@id/ImageView" 
    android:layout_alignTop="@id/ImageView" 
    android:layout_alignRight="@id/ImageView" 
    android:layout_alignBottom="@id/ImageView" 
    android:text=//u r text here 
    android:gravity="center" 
    /> 

Spero che questo potrebbe aiutare.

0

sufficiente trascinare e rilasciare il TextView sopra ImageView in Eclipse

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="48dp" 
    android:layout_marginTop="114dp" 
    android:src="@drawable/bluehills" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageView1" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="85dp" 
    android:text="TextView" /> 

</RelativeLayout> 

E questo l'output sopra xml enter image description here

+0

possiamo creare una mappa bit di questo? –

0

Come lei ha ricordato in OP, è necessario sovrapporre Text su ImageView programmazione strada.È possibile ottenere ImageView disegnabile e scrivere su di esso con l'aiuto di metterlo su Canvas e Paint.

private BitmapDrawable writeTextOnDrawable(int drawableId, String text) 
{ 
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true); 
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD); 
Paint paint = new Paint(); 
paint.setStyle(Style.FILL); 
paint.setColor(Color.WHITE); 
paint.setTypeface(tf); 
paint.setTextAlign(Align.CENTER); 
paint.setTextSize(11); 
Rect textRect = new Rect(); 
paint.getTextBounds(text, 0, text.length(), textRect); 
Canvas canvas = new Canvas(bm); 
canvas.drawText(text, xPos, yPos, paint); 
return new BitmapDrawable(getResources(), bm); 
} 
+0

Voglio mettere un testo sulla vista di immagini e voglio la libertà di trascinarlo sopra dove voglio. Qualcosa di simile fatto nell'applicazione "Phonto". – Mayur

6

è possibile utilizzare framelayout per raggiungere questo obiettivo.

come usare framelayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageView 
    android:src="@drawable/ic_launcher" 
    android:scaleType="fitCenter" 
    android:layout_height="250px" 
    android:layout_width="250px"/> 

    <TextView 
    android:text="Frame Demo" 
    android:textSize="30px" 
    android:textStyle="bold" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:gravity="center"/> 
</FrameLayout> 

ref: tutorialspoint

0

si può provare anche questo. Io uso solo framelayout.

<FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/cover" 
       android:gravity="bottom"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:text="Hello !" 
        android:id="@+id/welcomeTV" 
        android:textColor="@color/textColor" 
        android:layout_gravity="left|bottom" /> 
      </FrameLayout> 
-1

Forse dovresti scrivere prima ImageView e TextView secondo. Questo può aiutare a volte. È semplice, ma spero che aiuti qualcuno. :)