Wis*_*erd 1 android android-actionbar
我在我的Android应用程序中使用了一个Action Bar(一个常规的,而不是sherlock),当应用程序打开时,我想在操作栏中显示一条刷新的消息.这意味着我想要隐藏菜单项和标题(类似于GMail应用程序在刷新时的显示方式).
对此最好的方法是什么?它是否使用上下文操作栏?
是否可以在操作栏下方显示刷新动画,例如在GMail应用程序中(即蓝线滑过).
我知道我可以使用第三方pull-to-refresh,但我不想使用它(因为我不需要pull-to-refresh功能).
我的目标是Jelly Bean和更新的设备.
谢谢!
我想隐藏菜单项和标题(类似于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" />
Run Code Online (Sandbox Code Playgroud)
履行
/** 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;
}
Run Code Online (Sandbox Code Playgroud)
结果

结论
此实现与Gmail和其他应用程序非常相似,减去了拉动刷新模式.
当你打电话showLoadingMessage,张贴Runnable或使用View.OnClickListener.你不想WindowManager.addView太早打电话,或者你会扔掉一个WindowManager.BadTokenException.此外,它调用非常重要removeLoadingMessage的Activity.onDestroy,否则您将泄漏的风险,View您添加到Window.
| 归档时间: |
|
| 查看次数: |
666 次 |
| 最近记录: |