2013-01-02 93 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預覽版。