[英]How do I have an ActionBar icon/logo that overlaps the content as well?
我目前正在制作我的第一個應用程序。 我正在使用 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
您可以:
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);
盡管原始答案適用於某些設備,但在其他設備上,圖像位於狀態欄下方。 我通過獲取頂部 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)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.