【问题标题】:Showing refreshing message in Action Bar在操作栏中显示刷新消息
【发布时间】:2014-04-18 11:28:06
【问题描述】:

我在我的 android 应用程序中使用了一个操作栏(普通的,不是 sherlock),当应用程序打开时,我想在操作栏中显示一条令人耳目一新的消息。这意味着我想隐藏菜单项和标题(类似于 GMail 应用程序在刷新时的显示方式)。

对此最好的方法是什么?是否使用上下文操作栏?

是否可以在操作栏下方显示刷新动画,就像在 GMail 应用程序中一样(即,蓝线滑过)。

我知道我可以使用第 3 方的下拉刷新,但我不想使用它(因为我不需要下拉刷新功能)。

我的目标是 Jelly Bean 和更新的设备。

谢谢!

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    我想隐藏菜单项和标题(类似于 GMail 应用程序 刷新时出现)。

    这可以通过使用WindowManager.addView(View, LayoutParams) 来完成。这是一个在ActionBar 上显示消息的示例,它应该让您对如何继续有一个非常可靠的想法。

    布局

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="18sp" />
    

    实施

    /** The attribute depicting the size of the {@link ActionBar} */
    private static final int[] ACTION_BAR_SIZE = new int[] {
            android.R.attr.actionBarSize
    };
    
    /** The notification layout */
    private TextView mMessage;
    
    private void showLoadingMessage() {
        // Remove any previous notifications
        removeLoadingMessage();
    
        // Initialize the layout
        if (mMessage == null) {
            final LayoutInflater inflater = getLayoutInflater();
            mMessage = (TextView) inflater.inflate(R.layout.your_layout, null);
            mMessage.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
            mMessage.setText("Loading...");
        }
    
        // Add the View to the Window
        getWindowManager().addView(mMessage, getActionBarLayoutParams());
    }
    
    private void removeLoadingMessage() {
        if (mMessage != null && mMessage.getWindowToken() != null) {
            getWindowManager().removeViewImmediate(mMessage);
            mMessage = null;
        }
    }
    
    /**
     * To use, @see {@link WindowManager#addView(View, LayoutParams)}
     * 
     * @return The {@link WindowManager.LayoutParams} to assign to a
     *         {@link View} that can be placed on top of the {@link ActionBar}
     */
    private WindowManager.LayoutParams getActionBarLayoutParams() {
        // Retrieve the height of the status bar
        final Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        final int statusBarHeight = rect.top;
    
        // Retrieve the height of the ActionBar
        final TypedArray actionBarSize = obtainStyledAttributes(ACTION_BAR_SIZE);
        final int actionBarHeight = actionBarSize.getDimensionPixelSize(0, 0);
        actionBarSize.recycle();
    
        // Create the LayoutParams for the View
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                LayoutParams.MATCH_PARENT, actionBarHeight,
                WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP;
        params.x = 0;
        params.y = statusBarHeight;
        return params;
    }
    

    结果

    结论

    这种实现方式与 Gmail 和其他应用程序非常相似,只是缺少了下拉刷新模式。

    当您致电showLoadingMessage 时,请发帖Runnable 或使用View.OnClickListener。你不想太早打电话给WindowManager.addView,否则你会抛出WindowManager.BadTokenException。此外,在Activity.onDestroy 中调用removeLoadingMessage 也很重要,否则您将面临泄漏您添加到WindowView 的风险。

    【讨论】:

    • 谢谢。作为 Runnable 发布的目的是什么?
    • @WiseShepherd 你不想太早打电话给WindowManager.addView,否则你会抛出WindowManager.BadTokenException
    • @adneal,我可以为这个从 WindowManager.addview 创建的视图设置动画吗?我应该在哪里实现动画?
    • @JongzPuangput 是的,在您致电 WindowManager.addView 之后。
    • @adneal 谢谢你的回答。现在我正在尝试设置动画,但我在 android 中没有任何动画经验。但是,我正在尝试,但如果您能提供一些示例,那就太好了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2012-12-15
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    相关资源
    最近更新 更多