android.support.v4.widget.SlidingPaneLayout
スライドメニューの作成に使用しています。残念ながら、SlidingPaneLayout
メイン コンテンツのみをプッシュし、アクション バーは含まれません。このレイアウトでアクションバーもプッシュしたい!
次のようなレイアウトを希望します。
android.support.v4.widget.SlidingPaneLayout
スライドメニューの作成に使用しています。残念ながら、SlidingPaneLayout
メイン コンテンツのみをプッシュし、アクション バーは含まれません。このレイアウトでアクションバーもプッシュしたい!
次のようなレイアウトを希望します。
受け入れられた答えは、すべての Android api では機能しないと思います。私は以下のコードを書き、それはうまくいきます。
ツールバーに 2 つのボタン (左右に 1 つ) を設定し、画面のタイトルをツールバーの中央に設定しました。必要なものをすべてツールバーに簡単に追加できます。
<?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>
別の解決策があります。デフォルトの actionBar の代わりに setSupportActionBar を使用します。
<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();
}
ContentFragment.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;
}
fragment_content.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>
これがお役に立てば幸いです。