我在应用程序中使用NavigationDrawer
作为主菜单。我的一些片段使用SlidingPaneLayout
。
现在,我在右边显示NavigationDrawer
,在左边显示SlidingPaneLayout
,总是有点可见。
但我希望NavigationDrawer
在左侧,SlidingPaneLayout
在右侧(就像Hangouts中一样)总是有点可见。
问题:
我知道如何将NavigationDrawr
移到另一侧,但我不知道如何(如果可能的话)将SlidingPaneLayout
移到右侧?让它从右边滑进来。。。
我的解决方案
<android.support.v4.widget.SlidingPaneLayout
android:id="@+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/fragment_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_marginRight="0dp"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/card_toast_container" />
<FrameLayout
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- marginLeft: set it to width - 150 in your code!!! -->
<FrameLayout
android:id="@+id/fragment_slider"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_marginLeft="0dp" />
</android.support.v4.widget.SlidingPaneLayout>
并使用一些包装方法,例如以下方法,以获得更简单、更可读的代码:
public boolean isSliderMenuShown()
{
return !mSlidingLayout.isOpen();
}
public static void openSlider(boolean isSliderLeft, MySlidingPaneLayout slidingLayout)
{
slidingLayout.closePane();
}
public static void closeSlider(boolean isSliderLeft, MySlidingPaneLayout slidingLayout)
{
slidingLayout.openPane();
}
将其设置为在导航抽屉内查看:
android:layout_gravity="left"
你可以试着在开始时保持它的打开状态:
SlidingPaneLayout sp = (SlidingPaneLayout) findViewById(R.id.spl);
sp.openPane();
同时将您的主要内容保留在左侧,菜单保留在右侧
经过我的研究和测试,android.support.v4.widget.SlidingPaneLayout
无法从右向左滑动。唯一的效果是从左向右滑动。不管android:layout_gravity=
是start
left
end
还是right
。
您可以通过为Slidingpanel布局添加布局方向来实现这一点
layout = (SlidingPaneLayout) findViewById(R.id.sliding_pane_layout);
layout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
在此之前,请确保为清单文件添加了属性android:supportsRtl="true"
。
第1步
为了在您的应用程序中支持RTL,您首先需要添加android:supportsRtl=";真";到清单文件中的元素。
第2步
在SlidingPaneLayout 中添加RTL支持
android:layoutDirection="rtl";
第三步在SlidingPaneLayout 的子视图中添加LTR支持
android:layoutDirection="ltr";
<SlidingPane
android:id="@+id/slidingPanelLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<include
layout="@layout/left_drawer"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layoutDirection="ltr" />
<include
layout="@layout/view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
/>
</SlidingPane>
注意:在left_drawer.xml和view_container.xml 中添加LTR支持