【问题标题】:How do I make a sub view apear on top of an existing activity in Android?如何让子视图出现在 Android 中现有活动的顶部?
【发布时间】:2010-06-03 14:54:17
【问题描述】:

我正在编写一个允许用户访问其公司语音邮件系统的应用程序。语音邮件显示在列表视图中,选择一个菜单时,它们会显示一个菜单。当用户选择“收听”时,我希望在现有 ListView 顶部的屏幕底部弹出一个小型媒体播放器(类似于软键盘在需要时出现,然后在完成后消失)。

选项?

【问题讨论】:

    标签: android popupwindow android-listview


    【解决方案1】:

    将您的所有视图(即:您的 ListView)添加到 RelativeLayout,将您的媒体播放器布局元素放在底部,并将它们的可见性设置为消失。

    <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
    
      <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    
      <RelativeLayout
        android:id="@+id/mediaPopup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:visibility="gone"
        >
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="I am a media player!"
          />
      </RelativeLayout>
    
    </RelativeLayout>
    

    然后在您的活动类中,您可以像这样在视图上制作动画:

    function void showMediaPlayer() {
    
      if(mMediaPopUp.getVisibility() == View.GONE) {
    
        // Show Media Player
        TranslateAnimation mAnimUp = 
          new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 
            0, 
            Animation.RELATIVE_TO_SELF, 
            0, 
            Animation.RELATIVE_TO_SELF, 
            -(mMediaPopUp.getHeight()), 
            Animation.RELATIVE_TO_SELF, 
            0);
    
        mAnimUp.setStartOffset(500);
        mAnimUp.setDuration(500);
    
        mMediaPopUp.setVisibility(View.VISIBLE);
        mMediaPopUp.setAnimation(mAnimUp);
    
      }
    }
    

    【讨论】:

    • 太棒了!多谢。说我想在关闭弹出窗口时反转动画,那会是什么样子?
    • 它看起来很相似,只是您需要将 fromY 参数与 mAnimUp TranslateAnimation 的 toY 参数进行切换。此外,您可能需要使用 AnimationListener 并等到动画结束后再将 mMediaPopUp 视图可见性设置为 View.GONE
    • 如果您的 xml 在单独的 xml 文件中,您可以添加到上述答案中,您可以通过 LayoutInflator 加载它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2013-11-06
    相关资源
    最近更新 更多