與它自己掙扎了一段時間後,這裏的解決方案(測試了 - 工作好):
的一般步驟是:
- 創建包裝視圖
- 拆離屏幕查看兒童,放置包裝並附上兒童
- 將內容充滿兒童
- 控制包裝將幫助您精確控制操作欄及其下面的所有內容。
- 現在,使用包裝器,您可以將「兄弟」添加到操作欄/主區域。那個兄弟正是你在你的形象中描述的。
讓我們看看一些代碼。
首先,創建一個方法來幫助創建包裝視圖。包裝將被放置在整個屏幕和你的應用程序的內容之間。作爲ViewGroup,您稍後可以完全控制它的內容。
private ViewGroup setContentViewWithWrapper(int resContent) {
ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decorView.getChildAt(0);
// Removing decorChild, we'll add it back soon
decorView.removeAllViews();
ViewGroup wrapperView = new FrameLayout(this);
// You should set some ID, if you'll want to reference this wrapper in that manner later
//
// The ID, such as "R.id.ACTIVITY_LAYOUT_WRAPPER" can be set at a resource file, such as:
// <resources xmlns:android="http://schemas.android.com/apk/res/android">
// <item type="id" name="ACTIVITY_LAYOUT_WRAPPER"/>
// </resources>
//
wrapperView.setId(R.id.ACTIVITY_LAYOUT_WRAPPER);
// Now we are rebuilding the DecorView, but this time we
// have our wrapper view to stand between the real content and the decor
decorView.addView(wrapperView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
wrapperView.addView(decorChild, decorChild.getLayoutParams());
LayoutInflater.from(this).inflate(getActivityLayout(),
(ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(1), true);
return wrapperView;
}
現在,干擾了正規Activity創造,而不是使用setContentView,使用我們已經創建的方法。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// DON'T CALL `setContentView`,
// we are replacing that line with this code:
ViewGroup wrapperView = setContentViewWithWrapper(R.layout.activity_layout);
// Now, because the wrapper view contains the entire screen (including the notification bar
// which is above the ActionBar) I think you'll find it useful to know the exact Y where the
// action bar is located.
// You can use something like that:
ViewGroup actionBar = (ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(0);
int topOffset = actionBar.getTop();
// Now, if you'll want to add a view:
// 1. Create new view
// 2. Set padding top - use "topOffset"
// 3. Add the view to "wrapperView"
// 4. The view should be set at front. if not - try calling to "bringToFront()"
}
就是這樣。
注
編輯學到了很多東西:參考@CristopherOyarzúnAltamirano回答進一步的支持在較新的Android版本
祝你好運!
你找到一個解決這個問題?我想現在在我的應用程序中執行相同的任務,但我在下面看不到任何相關答案。 – 2013-03-25 23:01:23
@MM看看我的答案。剛添加它。 – Sean 2013-06-05 16:42:14