当我连接到服务器时(我正在开发SIP应用程序),我想显示图像。
我做了这个(创建了一个带有textview和图像的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>

最佳答案

您不应在一个活动中多次调用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>

关于android - 更改指令功能中的用户界面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5842351/

10-09 07:12
查看更多