android – 如何放置覆盖查看动作栏Sherlock

前端之家收集整理的这篇文章主要介绍了android – 如何放置覆盖查看动作栏Sherlock前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想设置一些视图,显示教程文本(像点击这里发送电子邮件…)的动作栏.这可能吗?我问,因为我知道动作栏使用布局的顶部空间,片段或活动使用剩余空间.
我的第二个问题是如何在动作栏上显示所有的动作项.我使用ActionBarSherlock库,我看到我有一个动作项目的空间,但它没有显示在操作栏上.我在项目中设置了xml ifRoom选项…

谢谢!!!

解决方法

有多种方法可以实现类似教程的叠加层.可能最简单的一个是使用专门准备的 Dialog窗口,透明背景,没有暗淡.

使用自定义对话框进行教程叠加

首先我们必须准备对话框的内容.在这个例子中,RelativeLayout里面会有一个TextView,这里是最有用的布局.

info_overlay.xml文件内容

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <TextView
  7. android:id="@+id/textView1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_alignParentTop="true"
  12. android:background="@android:color/darker_gray"
  13. android:padding="3dp"
  14. android:text="TextView"
  15. android:textColor="@android:color/white" />
  16.  
  17. </RelativeLayout>

现在,我们可以使用这个布局来创建我们的对话框:

  1. public class MainActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7.  
  8. Dialog overlayInfo = new Dialog(MainActivity.this);
  9. // Making sure there's no title.
  10. overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
  11. // Making dialog content transparent.
  12. overlayInfo.getWindow().setBackgroundDrawable(
  13. new ColorDrawable(Color.TRANSPARENT));
  14. // Removing window dim normally visible when dialog are shown.
  15. overlayInfo.getWindow().clearFlags(
  16. WindowManager.LayoutParams.FLAG_DIM_BEHIND);
  17. // Setting position of content,relative to window.
  18. WindowManager.LayoutParams params = overlayInfo.getWindow().getAttributes();
  19. params.gravity = Gravity.TOP | Gravity.LEFT;
  20. params.x = 100;
  21. params.y = 20;
  22. // If user taps anywhere on the screen,dialog will be cancelled.
  23. overlayInfo.setCancelable(true);
  24. // Setting the content using prepared XML layout file.
  25. overlayInfo.setContentView(R.layout.info_overlay);
  26. overlayInfo.show();
  27. }

结果

以下是上述解决方案的截图.注意TextView over ActionBar.

关于解决方案的几个注释

>如果你有专门的按钮来关闭教程,你可以使用setCancelable(false)来避免意外关闭教程.
>此解决方案适用于任何主题与任何动作栏解决方案(操作系统提供,Android支持库或ActionBar Sherlock)

其他解决方案/帮手

看看Showcase View library,因为它专注于以简单的方式创建类似教程的屏幕.我不知道它可以轻松地覆盖动作栏.

猜你在找的Android相关文章