1

我想在连接到服务器时显示图像(我正在开发 SIP 应用程序)。我做了这个(创建了一个带有文本视图和图像的 XML 文件[connected.xml]),但是有一个 FC:

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

然后我想在连接和断开连接时添加图像......我该如何解决这个问题?非常感谢。

编辑: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>
4

2 回答 2

1

您不应该setContentView在一项活动中多次调用,除非您 100% 确定您已经清理了其中的所有内容。
即便如此,您的用户可能会觉得奇怪,即使开始相同的活动,按下后退按钮他们也看不到他们在等待什么。

所以你应该考虑

  • 更改视图的可见性以显示/隐藏它的不同部分,或
  • 更优雅、更简单的解决方案:使用ViewFlipper.

更新 1
这是一个示例用法ViewFlipper
将这些行放入您的布局 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>

在你的java代码中,当你需要改变视图时,你写:

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

额外:您可以为您的翻转设置动画,您所要做的就是在调用之前设置您inout动画或:flippershowNextshowPrevious

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();

更新 2

ViewFlipper带有全局标头的示例:

<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>
于 2011-04-30T14:49:13.617 回答
0

可能是线程问题:UI 更改必须发生在 Activity 的主循环线程上。看看http://developer.android.com/reference/android/os/Handler.html

  1. 为活动创建一个处理程序。
  2. 注册完成后发送消息 ( http://developer.android.com/reference/android/os/Message.html )。
  3. 在处理程序中,接收该消息并更改您的 UI。

让我知道它是如何工作的。

编辑:修复了消息链接

于 2011-04-30T14:46:27.987 回答