I am using android.support.v4.widget.SlidingPaneLayout
to make a sliding menu. Alas, SlidingPaneLayout
only pushes main contents, and does not include the action bar. I want this layout to push the action bar too!
I would like the layout like this:
(Sorry for poor English. Please somebody edit this answer.)
Finally, I solved this problem! I inspired by this project
In short,
ActionbarActivity has view hierarchy like this (but different version has different hierarchy! this example is hierarchy of android 2.3 gingerbread.).
A(decorView)--- B(FrameLayout)--- C(LinearLayout)--- D(Layout including actionbar)
\__ E(Layout including contents)
B
from A
and result.
A(decorView)--- F(SlidingPaneLayout)--- G(sliding menu view)
\__ B(FrameLayout)--- C(LinearLayout)--- D(Layout including actionbar)
\__ E(Layout including contents)
application codes in below
MainActivity.java
public class MainActivity extends ActionBarActivity {
@InjectView(R.id.hello)
TextView mTextView;
@InjectView(R.id.call_menu)
Button mCallMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
//get A
ViewGroup parentView = (ViewGroup) getWindow().getDecorView();
//get B
ViewGroup viewIncludingAction = (ViewGroup) parentView.getChildAt(0);
//maintain background theme
TypedArray a = getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
int background = a.getResourceId(0, 0);
a.recycle();
viewIncludingAction.setBackgroundResource(background);
//remove B
parentView.removeView(viewIncludingAction);
//inflate F
final ViewGroup paneLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.view_slide_pane, null, false);
//inflate G
View menuView = getLayoutInflater().inflate(R.layout.fragment_side_menu, paneLayout, false);
//because there's no default padding for status bar, add it mint result = 0;
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
menuView.setPadding(0, result, 0, 0);
//process 4~6
paneLayout.addView(menuView);
paneLayout.addView(viewIncludingAction);
parentView.addView(paneLayout);
mCallMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((SlidingPaneLayout)paneLayout).openPane();
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/hello"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/call_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/hello"
android:text="pop"/>
</RelativeLayout>
fragment_side_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:text="TEST"/>
</LinearLayout>
view_slid_pane.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.widget.SlidingPaneLayout>