android - 更改指令功能中的用户界面

标签 android android-xml

我想在连接到服务器时显示图像(我正在开发 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在一项 Activity 中多次,除非你 100% 确定你已经清理了其中的所有内容。
即便如此,您的用户可能会觉得奇怪,按下后退按钮他们看不到他们在等待什么,尽管开始了相同的 Activity 。

所以你应该好好想想

  • 将您的 View 的可见性更改为 显示/隐藏它的不同部分,或者
  • 更优雅、更简单的解决方案: 使用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 代码中,当您需要更改 View 时,您可以编写:

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

额外: 你可以为你的翻转设置动画,你所要做的就是设置 inout你的动画 flipper打电话前showNextshowPrevious :

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 的示例带有全局 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>

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

相关文章:

android - 字符串资源中的 HTML

android - 图形布局显示空指针异常

android - 我应该选择 x86_64 进行测试,而不是选择 "recommended"x86 作为虚拟设备系统镜像吗?

android - 在 recyclerview firebase 中转换 arraylist

java - 按钮 onClickListener 在 Android 中无法播放声音

android - 在 Android 的锁定屏幕上显示警告对话框

android - 使用可样式属性时出现 NumberFormatException

java - 如何将参数从 Activity 类传递到 Android 中扩展 View 的类

android - 跟进 Android 应用程序的密码保护启动

android - XML android 中带有圆底的背景图像