3

I'm currently making one of my very first applications. I'm using ActionBarSherlock. I would like to make my logo overlap the actionbar (scrollview).

Currently I have main_activity.xml. In MainActivity.java I use setContentView to view main_activity.xml. After that I use getSupportActionBar() for ActionBarSherlock. I've tried things out using RelativeLayout  (http://www.mkyong.com/android/android-relativelayout-example/). That didn't really work because there are multiple layouts.

So I've tried some things right and left, but it always ends up infront or behind the actionbar, or stops just before reaching the content. It's because of two different layouts, that's what I know. But how can I going to solve this? Is it possible? Thanks in advance!

What I want: http://f.cl.ly/items/3N0w243N1t2Q3i1H1f1k/Untitled-1.png

4

2 回答 2

4

您可以:

A. 将您的图像一分为二
将顶部作为 ActionBar 徽标,然后将底部显示在您的内容上。

B. 使用单个图像
您需要一个仅包含您的徽标的布局文件(您可能需要像ImageViewinside aLinearLayout这样的东西,这样您就可以轻松设置正确的边距)。然后在调用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);
于 2013-01-02T23:54:45.367 回答
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);
                        }
                    }
                }
        );
    }

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

于 2014-07-02T10:26:15.987 回答