让Android“SlidingPaneLayout”也推动动作栏

问题描述 投票:2回答:4

我正在使用android.support.v4.widget.SlidingPaneLayout制作滑动菜单。唉,SlidingPaneLayout只推送主要内容,不包括动作栏。我希望这个布局也可以推动动作栏!

我想这样的布局:

android slidingmenu slidingpanelayout
4个回答
1
投票

(抱歉英语不好。请有人编辑这个答案。)

最后,我解决了这个问题!我受到this project的启发

简而言之,

ActionbarActivity具有这样的视图层次结构(但不同版本具有不同的层次结构!此示例是android 2.3 gingerbread的层次结构。)。

A(decorView)--- B(FrameLayout)--- C(LinearLayout)--- D(Layout including actionbar)
                                                 \__ E(Layout including contents)
  1. B删除A
  2. 膨胀SlidingPaneLayout(称为F)
  3. 膨胀滑动菜单的视图(称为G)
  4. 将G添加到F中
  5. 添加到F
  6. 将F添加到A中

和结果。

A(decorView)--- F(SlidingPaneLayout)--- G(sliding menu view)
                                    \__ B(FrameLayout)--- C(LinearLayout)--- D(Layout including actionbar)
                                                                         \__ E(Layout including contents)

应用程序代码如下

main activity.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>

0
投票

我有另一种解决方案。使用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>

main activity.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();
}

content fragment.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>

我希望这能帮到您。


0
投票

我认为接受的答案不适用于所有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>

0
投票

两个简单的步骤:

  1. 确保活动没有操作栏(manifest.xml) <activity android:name=".ActivityName" android:theme="@style/AppTheme.NoActionBar">
  2. 转到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>

就是这样,希望它有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.