Frage

Ich möchte ein Bild anzeigen, wenn ich mit dem Server verbunden bin (ich entwickle eine SIP -Anwendung). Ich habe dies erstellt (erstellt eine XML -Datei [Connected.xml] mit einem Textview und einem Bild), habe aber einen FC:

public void onRegistrationDone(String localProfileUri, long expiryTime) {
                        updateStatus("Registered to server.");
                        Log.d("SUCCEED","Registration DONE");
                        setContentView(R.layout.connected);
                      }

Ich möchte dann ein Bild hinzufügen, wenn ich mich verbinden und getrennt haben ... wie kann ich dieses Problem lösen? Vielen Dank.

Bearbeiten: xml:

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

<TextView
  android:id="@+id/sipLabel"
  android:textSize="20sp"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>

<ViewFlipper
    android:id="@+id/flipper" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">


  <ImageView android:id="@+id/disconnected" android:src="@drawable/disconnected" android:layout_below="@id/sipLabel" 
  android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"
    />
    </RelativeLayout>

    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">


  <ImageView android:id="@+id/connected" android:src="@drawable/connected" android:layout_below="@id/sipLabel" 
  android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"
    />
    </RelativeLayout>

</ViewFlipper>
</RelativeLayout>
War es hilfreich?

Lösung

Sie sollten nicht anrufen setContentView Mehrmals bei einer Aktivität, es sei denn, Sie sind zu 100% sicher, dass Sie alles darin aufgeräumt haben.
Selbst dann finden Ihre Benutzer seltsam, dass sie nicht sehen, was sie warten, obwohl sie die gleiche Aktivität starten.

Sie sollten also lieber darüber nachdenken

  • Ändern der Sichtbarkeit Ihrer Ansichten, um verschiedene Teile davon zu zeigen/zu verbergen, oder
  • Eine elegantere und einfachere Lösung: Verwenden Sie a ViewFlipper.

Update 1
Hier ist eine Stichprobenverwendung der ViewFlipper:
Legen Sie diese Zeilen in Ihr Layout XML:

<ViewFlipper android:id="@+id/flipper" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <!-- your first view comes here -->
    </RelativeLayout>
    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <!-- your second view comes here -->
    </RelativeLayout>
</ViewFlipper>

Und in Ihrem Java -Code, wenn Sie die Ansicht ändern müssen, schreiben Sie:

flipper = (ViewFlipper)findViewById(R.id.flipper);
flipper.showNext();

Extra: Sie können Ihr Flipping animieren. Alles, was Sie tun müssen, ist, das zu setzen in und out Animation von dir flipper voranrufen showNext oder showPrevious:

Animation inAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, +1.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f);
inAnimation.setDuration(250);
inAnimation.setInterpolator(new AccelerateInterpolator());

Animation outAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, -1.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f, 
        Animation.RELATIVE_TO_PARENT, 0.0f);
outAnimation.setDuration(250);
outAnimation.setInterpolator(new AccelerateInterpolator());

flipper = (ViewFlipper)findViewById(R.id.flipper);
flipper.setInAnimation(inAnimation);
flipper.setOutAnimation(outAnimation);
flipper.showNext();

Update 2:

Eine Probe für ViewFlipper mit globalem Header:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/status_text" android:text="Status: "
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <ViewFlipper android:id="@+id/flipper" android:layout_below="@id/status_text"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <RelativeLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
            <!-- your first view comes here without the status TextView -->
        </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
            <!-- your second view comes here -->
        </RelativeLayout>
    </ViewFlipper>
</RelativeLayout>

Andere Tipps

Wahrscheinlich ein Thread -Problem: UI -Änderungen müssen auf dem Hauptschleifenfaden der Aktivität stattfinden. Ansehen http://developer.android.com/reference/android/os/handler.html:

  1. Erstellen Sie einen Handler für die Aktivität.
  2. Eine Nachricht schicken (http://developer.android.com/reference/android/os/message.html) Wenn die Registrierung abgeschlossen ist.
  3. Empfangen Sie diese Nachricht im Handler und führen Sie Ihre Benutzeroberfläche ändern.

Lassen Sie mich wissen, wie das funktioniert.

Bearbeiten: Der Nachrichtenlink wurde behoben

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top