【问题标题】:How do I have an ActionBar icon/logo that overlaps the content as well?我如何拥有与内容重叠的 ActionBar 图标/徽标?
【发布时间】:2013-01-02 23:46:17
【问题描述】:

我目前正在制作我的第一个应用程序。我正在使用 ActionBarSherlock。 我想让我的徽标与操作栏(滚动视图)重叠。

目前我有 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

【问题讨论】:

标签: java android android-actionbar actionbarsherlock


【解决方案1】:

您可以:

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

B.使用单张图片
您将需要一个仅包含您的徽标的布局文件(您可能需要在LinearLayout 中添加类似ImageView 的内容,以便轻松设置正确的边距)。 然后在为您的活动调用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 布局一起使用?谢谢!
  • 使用示例布局文件编辑了我的答案。您的代码答案不适用于不同的分辨率,因为您正在以像素为单位设置布局的尺寸 - 如果您想在代码中使用它,然后将您的宽度/高度乘以当前比例 (float scale = getResources().getDisplayMetrics().density),例如logo.setLayoutParams(new LayoutParams((int) scale*89, (int) scale*175));
【解决方案2】:

虽然原始答案适用于某些设备,但在其他设备上,图像位于状态栏下方。我通过获取顶部 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)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多