如何在ActionBar上显示叠加在内容之上的图标/标志?

3

我正在制作我的第一个应用程序。我正在使用ActionBarSherlock。 我想让我的标志Logo重叠在操作栏(ScrollView)上。

目前我有main_activity.xml文件。在MainActivity.java中,我使用setContentView来查看main_activity.xml。之后,我使用getSupportActionBar()调用ActionBarSherlock。我尝试过使用RelativeLayout(http://www.mkyong.com/android/android-relativelayout-example/)进行实验。但是由于存在多个布局,在此方面的尝试并没有成功。

因此,我左右尝试了一些方法,但它总是出现在操作栏的前面或后面,或者刚好停在内容之前。这是由于存在两个不同的布局,这是我所知道的。但是我该如何解决呢?是否有可能?谢谢!

我想要的效果:http://f.cl.ly/items/3N0w243N1t2Q3i1H1f1k/Untitled-1.png


如果你搜索“Android layout z”,你会发现这个SO:https://dev59.com/questions/-m865IYBdhLWcg3wNLw7 - Morrison Chang
对我不起作用。无法重叠ActionBar。 - Milan van Schaik
2个回答

4
你可以选择以下两种方式: A. 将图像分为两部分
将顶部作为ActionBar的标志,然后将底部显示在内容上方。 B. 使用单个图像
你需要一个仅包含你的标志的布局文件(你可能想要像ImageView这样的视图,放在LinearLayout中,以便你可以轻松设置正确的边距)。 然后,在为你的活动调用setContentView之后,通过以下方式添加你的标志视图:
ViewGroup decorViewGroup = (ViewGroup) getWindow().getDecorView();
decorViewGroup.addView(logoView);

使用布局文件

示例布局文件(logo_view.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_image"
        android:scaleType="center"
        android:layout_marginLeft="10dip"
        />

</LinearLayout>

解压布局文件:

LayoutInflater inflater = LayoutInflater.from(this);
View logoView = inflater.inflate(R.layout.logo_view, null, false);

谢谢!这个可以用,但是我无法完全让它工作。ViewGroup decorViewGroup = (ViewGroup) getWindow().getDecorView(); ImageView logo = new ImageView(this); logo.setImageResource(R.drawable.actionbar_logo); logo.setLayoutParams(new LayoutParams(89, 175)); decorViewGroup.addView(logo);我无法使用XML布局使其正常工作,所以我在文件中创建了一个ImageView。但现在我无法让它适应多种分辨率等。如何让它与XML布局一起工作?谢谢! - Milan van Schaik
编辑了我的答案,并附上了一个示例布局文件。你的代码答案不能在不同分辨率下工作,因为你正在以像素为单位设置布局的尺寸——如果你想在代码中实现它,那么请将你的宽度/高度乘以当前比例(float scale = getResources().getDisplayMetrics().density),例如logo.setLayoutParams(new LayoutParams((int) scale*89, (int) scale*175)); - Joseph Earl

1
虽然原始答案在一些设备上有效,但在其他设备上,图像会放在状态栏下。我通过获取顶部 ActionBar 的位置并将其与标志图像顶部的位置进行比较,然后只需添加一些顶部填充来解决此问题,如下所示:
    // Inflate logo layout
    LayoutInflater inflater = LayoutInflater.from(this);
    final View logoView = inflater.inflate(R.layout.menu_logo, null);

    // Add logo to view
    ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
    viewGroup.addView(logoView);

    // Adjust the logo position
    int resId = getResources().getIdentifier("action_bar_container", "id", "android");
    final View actionBarView = viewGroup.findViewById(resId);
    if (actionBarView != null) {
        actionBarView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    public void onGlobalLayout() {
                        // Remove the listener
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            actionBarView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        } else {
                            actionBarView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }

                        // Measure views
                        int[] location = new int[2];
                        actionBarView.getLocationOnScreen(location);

                        int[] logoLocation = new int[2];
                        logoView.getLocationOnScreen(logoLocation);

                        // Add top padding if necessary
                        if (location[1] > logoLocation[1]) {
                            logoView.setPadding(0, location[1] - logoLocation[1], 0, 0);
                        }
                    }
                }
        );
    }

这适用于广泛的设备(手机、大/小平板电脑 - 包括 Kindle Fire HDX)运行 Android 版本 4.0 至 4.4.4,以及 Android L 预览版。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接