2013-01-02 39 views
3

我目前正在做我的第一个应用程序之一。我正在使用ActionBarSherlock。 我想让我的商标与操作栏(scrollview)重叠。如何获得与内容重叠的ActionBar图标/徽标?

目前我有main_activity.xml。在MainActivity.java中,我使用setContentView来查看main_activity.xml。之后,我使用ActionBarSherlock的getSupportActionBar()。我使用RelativeLayout(http://www.mkyong.com/android/android-relativelayout-example/)尝试了一些东西。这并没有真正奏效,因为有多种布局。

所以我尝试了一些权利和离开的东西,但它总是在面前或后面的行动栏,或者在到达内容之前停止。这是因为两种不同的布局,这就是我所知道的。但我怎么能解决这个问题?可能吗?提前致谢!

我想要什么: http://f.cl.ly/items/3N0w243N1t2Q3i1H1f1k/Untitled-1.png

+0

如果你在'Android layout z'上搜索,你会发现这个:http://stackoverflow.com/questions/3929412/android-layout-layers-z-axis –

+0

不适用于我。无法重叠ActionBar。 –

回答

4

您可以:

A.分裂您的图像中的两个
有顶部的动作条标志,然后显示在你的内容底部。

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); 
+0

谢谢!这有效,但我无法完全发挥作用。 'ViewGroup decorViewGroup =(ViewGroup)getWindow()。getDecorView(); \t \t ImageView logo = new ImageView(this); \t \t logo.setImageResource(R.drawable.actionbar_logo); \t \t标志。setLayoutParams(new LayoutParams(89,175)); \t \t decorViewGroup.addView(logo);' 我无法使用XML布局工作,所以我在文件中创建了一个ImageView。但是现在我无法使用多个分辨率等工作。我如何让它使用XML布局?谢谢! –

+0

用示例布局文件编辑我的答案。您的代码答案不适用于不同的分辨率,因为您正在以像素为单位设置布局的尺寸 - 如果您想在代码中使用,请将宽度/高度乘以当前比例('float scale = getResources()。getDisplayMetrics ).density'),例如'logo.setLayoutParams(new LayoutParams((int)scale * 89,(int)scale * 175));' –

+0

非常感谢! XML布局文件选项做到了! –

1

尽管原始答案适用于某些设备,但在其他设备上,图像位于状态栏下。我解决了这个通过获取顶级动作条的位置,并将其与该标志图像的顶部的位置,然后只是增加了一些顶级的填充,如下:

// 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预览版。