这样的功能在Android中能实现吗?我的猜测是不行,因为你可以为ActionBar和Activity指定两个不同的布局,但我也可能错了。
PopupWindow
更可靠的解决方案。ImageView
,我们已将 clickable
属性设置为 true
,以防止触摸事件传播到下面的 View
。
logo.xml
布局:<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"
android:clickable="true" />
ActionBar
一起使用,并依赖于主页 View
作为覆盖层的锚点。但是,它可以很容易地修改为与支持 Toolbar
类和其徽标 View
一起使用,尽管这可能是不必要的,因为 Toolbar
类是一个可以在布局 XML 中轻松设置和操作的 View
。private void showLogoOverlay() {
final View anchor = findViewById(android.R.id.home);
if(anchor == null) {
return;
}
final View overlay = getLayoutInflater().inflate(R.layout.logo, null, false);
final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
anchor.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop,
int oldRight, int oldBottom) {
int[] offset = new int[2];
anchor.getLocationOnScreen(offset);
decor.addView(overlay, 200, 200);
overlay.setX(offset[0]);
overlay.setY(offset[1]);
anchor.removeOnLayoutChangeListener(this);
}
}
);
}