我android.support.v4.widget.SlidingPaneLayout
用来制作滑动菜单。唉,SlidingPaneLayout
只推送主要内容,不包括操作栏。我也想要这个布局来推动操作栏!
我想要这样的布局:
我android.support.v4.widget.SlidingPaneLayout
用来制作滑动菜单。唉,SlidingPaneLayout
只推送主要内容,不包括操作栏。我也想要这个布局来推动操作栏!
我想要这样的布局:
(抱歉英语不好。请有人编辑这个答案。)
最后,我解决了这个问题!我受到这个项目的启发
简而言之,
ActionbarActivity 有这样的视图层次结构(但不同的版本有不同的层次结构!这个例子是 android 2.3 姜饼的层次结构。)。
A(decorView)--- B(FrameLayout)--- C(LinearLayout)--- D(Layout including actionbar)
\__ E(Layout including contents)
B
_A
和结果。
A(decorView)--- F(SlidingPaneLayout)--- G(sliding menu view)
\__ B(FrameLayout)--- C(LinearLayout)--- D(Layout including actionbar)
\__ E(Layout including contents)
下面的应用代码
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>
我有另一个解决方案。使用 setSupportActionBar 而不是默认的 actionBar。
首先添加<item name="windowActionBar">false</item>
到您的主题中。
activity_main.xml
<android.support.v4.widget.SlidingPaneLayout
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:orientation="vertical"
android:id="@+id/sliding_pane_layout">
<FrameLayout
android:id="@+id/frameLeft"
android:layout_width="300dp"
android:clickable="true"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="@+id/frameRight"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
//setSupportActionBar((Toolbar)findViewById(R.id.main_toolbar));
SlidingPaneLayout slidingPaneLayout = (SlidingPaneLayout)findViewById(R.id.sliding_pane_layout);
slidingPaneLayout.setPanelSlideListener(this);
slidingPaneLayout.setCoveredFadeColor(0xffff0000);
Fragment fragment1 = new SideFragment();
Fragment fragment2 = new ContentFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frameLeft, fragment1);
ft.replace(R.id.frameRight, fragment2);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
ft.commit();
}
内容片段.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_content, container, false);
ActionBarActivity a = (ActionBarActivity)getActivity();
a.setSupportActionBar((Toolbar) view.findViewById(R.id.main_toolbar));
return view;
}
片段内容.xml
<LinearLayout
android:background="@color/red_800"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
xmlns:sothree="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_toolbar"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
sothree:theme="@style/ActionBar"
android:layout_width="match_parent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
我希望这能帮到您。
我认为接受的答案不适用于所有 Android api。我写了下面的代码,它工作正常。
在我的工具栏中,我设置了两个按钮(一个左一个右),并且我在工具栏的中心设置了屏幕的标题。在工具栏中添加您想要的所有内容很容易。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- left slide panel contains the ListView -->
<ListView
android:id="@+id/left_panel"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@null"
android:dividerHeight="0dp" />
<!-- right panel which contains content of fragment & toolbar -->
<FrameLayout
android:id="@+id/right_panel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- toolbar -->
<FrameLayout
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<!-- menu button -->
<Button
android:id="@+id/main_toolbar_menu_button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="start|center_vertical"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:background="@drawable/sliding_panel_button" />
<!-- toolbar title -->
<TextView
android:id="@+id/main_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- messages button -->
<Button
android:id="@+id/main_toolbar_messages_button"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="end|center_vertical"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/action_bar_messages"
android:visibility="gone" />
</FrameLayout>
</FrameLayout>
</android.support.v4.widget.SlidingPaneLayout>
</android.support.design.widget.CoordinatorLayout>
两个简单的步骤:
确保活动没有操作栏(manifest.xml)
<activity
android:name=".ActivityName"
android:theme="@style/AppTheme.NoActionBar">
转到您的右侧窗格(第二个孩子)SlidingPaneLayout
并 Toolbar
作为第一个孩子。就像是:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
...
</LinearLayout>
就是这样,希望它有帮助。