我想在操作栏上设置一些视图来显示教程文本(例如单击此处并发送电子邮件...)。这可能吗?我问是因为我知道操作栏使用布局上的顶部空间,而片段或活动使用剩余空间。
我的第二个问题是如何在操作栏上显示所有操作项。我使用 ActionBarSherlock 库,我发现我还有空间容纳另一个操作项,但它没有显示在操作栏上。我在项目的 xml ifRoom 选项中设置...
谢谢!!!
有多种方法可以实现类似教程的叠加。可能最简单的一种是使用专门准备的Dialog http://developer.android.com/guide/topics/ui/dialogs.html窗口具有透明背景且后面没有暗淡。
使用自定义Dialog
用于教程叠加
首先我们要准备好内容Dialog
。在这个例子中将会有一个TextView
inside RelativeLayout
这是这里最有用的布局。
内容info_overlay.xml
file:
<?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>
现在,我们可以使用这个布局来创建我们的Dialog
:
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();
}
Result
下面是上述解决方案工作的屏幕截图。请注意TextView
over ActionBar
.
关于解决方案的一些注意事项
- 如果您有一个专用按钮来关闭教程,您可以使用
setCancelable(false)
以避免教程意外关闭。
- 该解决方案适用于任何主题和任何操作栏解决方案(操作系统提供的,Android 支持库 or 动作栏夏洛克)
其他解决方案/帮助者
看一眼展示查看库 https://github.com/Espiandev/ShowcaseView因为它专注于以简单的方式创建类似教程的屏幕。但我不确定它是否可以轻松覆盖操作栏。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)