【问题标题】:How to create a view similar to the system notification area in android?如何在android中创建类似于系统通知区域的视图?
【发布时间】:2013-08-26 18:44:03
【问题描述】:

我想在我的活动中显示一个视图,它最初像一个小条一样位于屏幕顶部,但是当您点击它时它应该会向下展开,就像系统通知区域一样。

我还没有找到任何具有这种行为的标准控件。实现这一点的最佳方法是什么?

【问题讨论】:

标签: android animation drop-down-menu


【解决方案1】:

使用SlidingDrawer。这是一个很好的tutorial

【讨论】:

    【解决方案2】:

    SlidingDrawer 正是以这种方式工作的。

    【讨论】:

      【解决方案3】:

      问题在于 SlidingDrawer 不能定位在屏幕顶部——它只能向上打开 (see related question)。所以我自己实现了一个简单的控件,使用 TranslateAnimation

      class MySlidingDrawer extends LinearLayout {
          public static final int STATE_OPENED = 0;
          public static final int STATE_CLOSED = 1;
      
          private int m_intState;
          private LinearLayout m_content;
          private ImageButton m_handle;
      
          public MySlidingDrawer(Context context) {
              super(context);
              setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
              setOrientation(VERTICAL);
              setGravity(Gravity.CENTER);
      
              m_content = new LinearLayout(context);
                  // add your content here
              addView(m_content);
              m_intState = STATE_CLOSED;
      
              m_handle = new ImageButton(context);
              m_handle.setImageResource(R.drawable.icon);
              m_handle.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      toggleState();
                  }
              });
              m_handle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
              addView(m_handle);
          }
          private int getContentHeight() {
              return m_content.getHeight();
          }
          private void toggleState() {
              int intYStart = 0;
              int intYEnd = m_intState == STATE_OPENED ? -getContentHeight() : getContentHeight();
              Animation a = new TranslateAnimation(0.0f, 0.0f, intYStart, intYEnd);
              a.setDuration(1000);
              a.setStartOffset(300);
              a.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.bounce_interpolator));
              startAnimation(a);
              m_intState = m_intState == STATE_OPENED ? STATE_CLOSED : STATE_OPENED;
          }
      
          protected void onLayout(boolean changed, int l, int t, int r, int b) {
              super.onLayout(changed, l, t, r, b);
              offsetTopAndBottom(-getContentHeight()); // content is initially invisible
          }
          protected void onAnimationEnd() {
              super.onAnimationEnd();
              int intYOffset = m_intState == STATE_OPENED ? getContentHeight() : -getContentHeight(); 
              offsetTopAndBottom(intYOffset);
          }
      }
      

      【讨论】:

      • 您的代码运行良好,非常感谢。我确实注意到,当动画完成时,视图有点“闪烁”。某种人工制品,或者类似的东西……你也注意到了吗?
      【解决方案4】:

      您可以使用此答案中发布的代码:Android SlidingDrawer from top?

      提供的解决方案的特点是在 xml 中设置 Slidingdrawer 的方向,而且它很简单,只需要 1 个类和 attrs.xml 中的一些附加内容,并且稳定,因为它来自 SDK 的 Androids Slidingdrawer。我还讨论了为什么我没有选择在 Internet/SO 上找到的其他流行的库/解决方案。

      快速链接到要点:MultipleOrientationSlidingDrawer (source & example) @ gist

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-02
        • 1970-01-01
        • 2012-06-04
        • 2017-02-03
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        相关资源
        最近更新 更多