android - 应用程序 Logo 一半在 ActionBar 中,一半在 mainActivty 屏幕上

标签 android android-layout

在Android中可以实现这样的功能吗?我猜不是,因为您可以指定 2 种不同的布局,一种用于 ActionBar,一种用于 Activity,但我也可能弄错了

enter image description here

最佳答案

这是一个比之前建议的 PopupWindow 更可靠的解决方案。

Logo 叠加层的布局是一个简单的 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 作为覆盖的 anchor 。但是,它很容易修改以与支持的 Toolbar 类及其 Logo 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);
            }
        }
    );
}

关于android - 应用程序 Logo 一半在 ActionBar 中,一半在 mainActivty 屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25176096/

相关文章:

android - 从 onContextItemSelected() 上的列表获取值?

android - 使 View 与 AppBarLayout 重叠

android - ViewPager setCurrentItem 卡住 UI 线程

android - 如何将滚动事件从 View 传递到 CoordinatorLayout?

android - java.lang.NoSuchMethodError : okhttp3.内部.http.HttpEngine.recover

android - 带谷歌地图的 DrawerLayout

java - 带有全局变量的 TextView

android - 从 xml 文件中读取是否使应用程序变小?

android - 使用参数以编程方式添加按钮?

android - 提示文本在 Android 2.3 版本中看不到,但它适用于 4.1