显示清爽的消息在操作栏 [英] Showing refreshing message in Action Bar
问题描述
我在我的Android应用程序使用操作栏(正规的,不是福尔摩斯),以及应用程序打开时我要显示在操作栏中清爽的消息。这意味着我要隐藏的菜单项和标题(类似于如何时,它的刷新Gmail应用出现)。
,这是什么最好的方法?使用上下文操作栏是什么呢?
是否有可能表现出令人耳目一新的动画正下方的操作栏,就像在Gmail应用程序(即蓝线滑过)。
我知道我可以使用第三方拉来刷新,但我preFER不使用这个(因为我并不需要拉来刷新能力)。
我针对果冻豆和新设备。
谢谢!
我要隐藏的菜单项和标题(类似于Gmail的如何应用 当它的出现刷新)。
这可以通过使用 WindowManager.addView(查看,的LayoutParams)完成
。下面是在顶部显示消息的一个例子动作条
,应该给大家介绍一下如何进行一个pretty的坚实的想法。
布局
< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID /邮件
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:重力=中心
机器人:文字颜色=@机器人:彩色/白
机器人:TEXTSIZE =18sp/>
执行
/ **属性描述的大小{@link动作条} * /
私有静态最终诠释[] ACTION_BAR_SIZE =新INT [] {
android.R.attr.actionBarSize
};
/ **通知布局* /
私人TextView的mMessage;
私人无效showLoadingMessage(){
//移除任何previous通知
removeLoadingMessage();
//初始化布局
如果(mMessage == NULL){
最后LayoutInflater充气= getLayoutInflater();
mMessage =(TextView中)inflater.inflate(R.layout.your_layout,NULL);
mMessage.setBackgroundColor(getResources()的getColor(android.R.color.holo_blue_dark));
mMessage.setText(载入中...);
}
//视图添加到窗口
。getWindowManager()addView(mMessage,getActionBarLayoutParams());
}
私人无效removeLoadingMessage(){
如果(mMessage = NULL和放大器;!&安培;!mMessage.getWindowToken()= NULL){
。getWindowManager()removeViewImmediate(mMessage);
mMessage = NULL;
}
}
/ **
*使用,@see {@link窗口管理#addView(查看,的LayoutParams)}
*
* @返回的{@link WindowManager.LayoutParams}分配给一个
* {@link视图},可以被放置在{@link动作条}的顶
* /
私人WindowManager.LayoutParams getActionBarLayoutParams(){
//获得状态栏的高度
最后的矩形RECT =新的矩形();
。getWindow()getDecorView()getWindowVisibleDisplayFrame(RECT)。
最终诠释statusBarHeight = rect.top;
//检索动作条的高度
最后TypedArray actionBarSize = obtainStyledAttributes(ACTION_BAR_SIZE);
最终诠释actionBarHeight = actionBarSize.getDimensionPixelSize(0,0);
actionBarSize.recycle();
//创建的LayoutParams的视图
最后WindowManager.LayoutParams PARAMS =新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;
返回PARAMS;
}
结果
结论
这实现非常相似,Gmail等应用程序,减去拉来刷新模式。
当你调用 showLoadingMessage
发表的Runnable
或使用 View.OnClickListener
。你不想叫 WindowManager.addView
太早,否则会抛出一个 WindowManager.BadTokenException
。此外,它调用的重要 removeLoadingMessage
在 Activity.onDestroy
,否则你运行泄漏<$ C $的风险C>查看添加到窗口
。
I'm using an Action Bar (a regular one, not sherlock) in my android app, and when the app opens I want to show a refreshing message in the action bar. This means I want to hide the menu items and title (similar to how the GMail app appears when it's refreshing).
What is the best approach for this? Is it using a contextual action bar?
Is it possible to show the refreshing animation just below the action bar, like in the GMail app (ie, the blue lines sliding over).
I know I can use a 3rd party pull-to-refresh, but I'd prefer not to use this (as I don't need the pull-to-refresh capability).
I'm targeting Jelly Bean and newer devices.
Thanks!
I want to hide the menu items and title (similar to how the GMail app appears when it's refreshing).
This can be done by using WindowManager.addView(View, LayoutParams)
. Here's an example of displaying a message on top of the ActionBar
that should give you a pretty solid idea about how to proceed.
The layout
<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" />
Implementation
/** 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;
}
Results
Conclusion
This implementation is very similar to Gmail and other apps, minus the pull-to-refresh pattern.
When you call showLoadingMessage
, post a Runnable
or use a View.OnClickListener
. You don't want to call WindowManager.addView
too early or you'll throw a WindowManager.BadTokenException
. Also, it's important to call removeLoadingMessage
in Activity.onDestroy
, otherwise you run the risk of leaking the View
you add to the Window
.
这篇关于显示清爽的消息在操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!