我想设置一些视图,显示教程文本(像点击这里发送电子邮件…)的动作栏.这可能吗?我问,因为我知道动作栏使用布局的顶部空间,片段或活动使用剩余空间.
我的第二个问题是如何在动作栏上显示所有的动作项.我使用ActionBarSherlock库,我看到我有一个动作项目的空间,但它没有显示在操作栏上.我在项目中设置了xml ifRoom选项…
我的第二个问题是如何在动作栏上显示所有的动作项.我使用ActionBarSherlock库,我看到我有一个动作项目的空间,但它没有显示在操作栏上.我在项目中设置了xml ifRoom选项…
谢谢!!!
解决方法
有多种方法可以实现类似教程的叠加层.可能最简单的一个是使用专门准备的
Dialog窗口,透明背景,没有暗淡.
使用自定义对话框进行教程叠加
首先我们必须准备对话框的内容.在这个例子中,RelativeLayout里面会有一个TextView,这里是最有用的布局.
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:background="@android:color/darker_gray"
- android:padding="3dp"
- android:text="TextView"
- android:textColor="@android:color/white" />
- </RelativeLayout>
现在,我们可以使用这个布局来创建我们的对话框:
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Dialog overlayInfo = new Dialog(MainActivity.this);
- // Making sure there's no title.
- overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
- // Making dialog content transparent.
- overlayInfo.getWindow().setBackgroundDrawable(
- new ColorDrawable(Color.TRANSPARENT));
- // Removing window dim normally visible when dialog are shown.
- overlayInfo.getWindow().clearFlags(
- WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- // Setting position of content,relative to window.
- WindowManager.LayoutParams params = overlayInfo.getWindow().getAttributes();
- params.gravity = Gravity.TOP | Gravity.LEFT;
- params.x = 100;
- params.y = 20;
- // If user taps anywhere on the screen,dialog will be cancelled.
- overlayInfo.setCancelable(true);
- // Setting the content using prepared XML layout file.
- overlayInfo.setContentView(R.layout.info_overlay);
- overlayInfo.show();
- }
结果
以下是上述解决方案的截图.注意TextView over ActionBar.
关于解决方案的几个注释
>如果你有专门的按钮来关闭教程,你可以使用setCancelable(false)来避免意外关闭教程.
>此解决方案适用于任何主题与任何动作栏解决方案(操作系统提供,Android支持库或ActionBar Sherlock)
其他解决方案/帮手
看看Showcase View library,因为它专注于以简单的方式创建类似教程的屏幕.我不知道它可以轻松地覆盖动作栏.