2012-02-06 19 views
6

Im muy nuevo en este desarrollo de Android. Acabo de comenzar a crear una aplicación de hello world.Cómo establecer una imagen de fondo en android

Intenté establecer una imagen de fondo y está funcionando bien. Cuando intenté agregar un texto en la imagen, no funcionaba.

Intenté en la forma a continuación. ¿Alguien puede por favor ayudarme donde estoy yendo mal.

en

Main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/andriod" 
    android:layout_gravity="center_vertical" 
    android:gravity="center_vertical"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

</LinearLayout> 

HelloActivity.java

import android.app.Activity; 
    import android.os.Bundle; 
    import android.widget.TextView; 
    //import android.widget.ImageView;  

    public class HelloActivity extends Activity { 
    // /** Called when the activity is first created. */ 
    // @Override 
    // public void onCreate(Bundle savedInstanceState) { 
    //  super.onCreate(savedInstanceState); 
    //  setContentView(R.layout.main); 
    // }  

    /** Called when the activity is first created. */ 
     @Override 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      TextView tv = new TextView(this);    
      tv.setText("This is the first andorid app");   
      setContentView(R.layout.main);   
     } 
} 

Cuando se ejecuta está mostrando la imagen de fondo, pero "Esta es la primera aplicación Andorid" no se muestra.

Esperando por su ayuda

+3

@ishhhh: Está configurando texto en TextView TV, pero no agrega este televisor en el diseño. – Mudassir

Respuesta

3

Debe seleccionar TextView en su archivo de diseño con su ID único y luego establecer sus propiedades. Pero en el código ur, simplemente crea una vista de texto con el texto y no lo coloca en la vista.

TextView tv = (TextView) findViewById(R.id.textView1); 
tv.setText("Your text here"); 
+0

Muchas gracias friend.it funcionó para mí – ishhhh

+0

por favor dígame cómo puedo ejecutar los proyectos de muestra de Android descargados en el sdk.when descargo los proyectos de muestra no puedo ejecutarlo.puede ayudarme por favor – ishhhh

+0

de nada. si tiene eclipse instalado, en el asistente de proyectos de Android, hay una opción que verá que abre proyectos de muestra directamente en Eclipse. – NotCamelCase

4

Usted tendrá que obtener el TextView desde el punto de vista de contenido actual .. añadir estas líneas después de setContentView (..), también añadir un identificador a la vista de texto en el diseño

TextView tv = (TextView)findViewById(...) 
+0

agree.nice respuesta. –

+0

gracias rajdeep – ishhhh

6

primer lugar usted necesita para dar el TextView un id en el archivo XML, por lo que se puede alcanzar en la fuente de Java (tenga en cuenta el atributo android: id en siguiente XML);

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/andriod" 
    android:layout_gravity="center_vertical" 
    android:gravity="center_vertical"> 

    <TextView 
     android:id="@+id/textView" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

</LinearLayout> 

Ahora configure el texto en esta vista de texto;

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TextView textView = (TextView) findViewId(R.id.textView); 

    textView.setText("This is the first andorid app"); 
} 

Nota, cómo se accede a la vista de texto declarada en XML desde aquí.

No es

TextView textView = new TextView(this); 

esto creará un nuevo TextView. Pero lo que necesitamos aquí es

TextView textView = (TextView) findViewId(R.id.textView); 

esto se referirá a la vista de texto declarada en el archivo XML anterior.

+0

Gracias por su explicación detallada. Es muy útil para un principiante como yo. Espero su ayuda en el futuro también – ishhhh

+0

@ishhhh: Claro, y gracias por el voto popular. – Mudassir

+0

: ¿podría decirme cómo puedo ejecutar los proyectos de muestra de Android descargados en el SDK?cuando descargo los proyectos de muestra, no puedo ejecutarlo. ¿Pueden ayudarme? – ishhhh

Cuestiones relacionadas