Android 如何使用抽屉布局左侧移动主要内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20057084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move main content with Drawer Layout left side
提问by Mr. Cat
回答by nsL
If you dont want to use third-party libraries, you can implement it yourself just overriding the onDrawerSlide from the ActionBarDrawerToggle. There you can translate your framelayout view based on the opening % of your drawer.
如果您不想使用第三方库,您可以自己实现它,只需覆盖 ActionBarDrawerToggle 中的 onDrawerSlide。在那里,您可以根据抽屉的打开百分比来翻译您的框架布局视图。
Example with code:
代码示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>
And here, override onDrawerSlide:
在这里,覆盖 onDrawerSlide:
public class ConfigurerActivity extends ActionBarActivity
{
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private FrameLayout frame;
private float lastTranslate = 0.0f;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
frame = (FrameLayout) findViewById(R.id.content_frame);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.acc_drawer_open, R.string.acc_drawer_close)
{
@SuppressLint("NewApi")
public void onDrawerSlide(View drawerView, float slideOffset)
{
super.onDrawerSlide(drawerView, slideOffset);
float moveFactor = (mDrawerList.getWidth() * slideOffset);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
frame.setTranslationX(moveFactor);
}
else
{
TranslateAnimation anim = new TranslateAnimation(lastTranslate, moveFactor, 0.0f, 0.0f);
anim.setDuration(0);
anim.setFillAfter(true);
frame.startAnimation(anim);
lastTranslate = moveFactor;
}
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
// ... more of your code
}
}
Since setTranslationX is not available in pre-honeycomb android versions, i managed it using TranslateAnimation for lower version devices.
由于 setTranslationX 在 pre-honeycomb android 版本中不可用,我使用 TranslateAnimation 管理它以用于较低版本的设备。
Hope it helps!
希望能帮助到你!
回答by Nikola Despotoski
You might want to use this library of Drawer TogglesI wrote.
你可能想使用我写的这个抽屉开关库。
I'm sure you will find ContentDisplaceDrawerTogglehandy:
我相信你会觉得ContentDisplaceDrawerToggle很方便:
ContentDisplaceDrawerToggle mContentDisplaceToggle = new ContentDisplaceDrawerToggle(this, mDrawerLayout, R.id.content_frame);
mDrawerLayout.setDrawerListener(mContentDisplaceToggle);
ContentDisplaceDrawerToggledoes exactly what you are saying. It moves the content view as you slide in/out the DrawerLayout.
ContentDisplaceDrawerToggle完全按照你说的做。当您滑入/滑出DrawerLayout.


If you want to combine different toggles you can use the ActionBarToggleWrapperor DrawerToggleWrapper
如果您想组合不同的切换,您可以使用ActionBarToggleWrapper或DrawerToggleWrapper
Usage options are given in the read me file.
使用选项在自述文件中给出。
回答by Ness Tyagi
Here is working code...
这是工作代码...
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
mContainerFrame.setTranslationX(slideOffset * drawerView.getWidth());
mDrawerLayout.bringChildToFront(drawerView);
mDrawerLayout.requestLayout();
//below line used to remove shadow of drawer
mDrawerLayout.setScrimColor(Color.TRANSPARENT);
}//this method helps you to aside menu drawer
};
回答by Arash
OP got the answer. But for someone else that wants that effect, can use SlidingPaneLayout. It's designed for this purpose.
OP得到了答案。但是对于想要这种效果的其他人,可以使用SlidingPaneLayout。它是为此目的而设计的。
In XML file:
在 XML 文件中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/mainFrame"
style="@style/MP.mainFrame" >
<!--****************************Right Pane ****************************-->
<LinearLayout style="@style/searchLayout">
<android.support.v4.widget.NestedScrollView style="@style/MP">
<LinearLayout style="@style/MP.verticalLinearLayout">
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<!--****************************Right Pane ****************************-->
<!--****************************Left Pane ****************************-->
<FrameLayout style="@style/MP.mainLayout">
<LinearLayout android:id="@id/fragmentContainer" style="@style/MP.fragmentContainer"/>
<android.support.v7.widget.Toolbar style="@style/toolbar">
<ir.tooskar.excomponents.ExtendedTextView android:id="@id/appTitle" style="@style/WC.appTitle"/>
<ir.tooskar.excomponents.ExtendedTextView android:id="@id/appBarSearchIcon" style="@style/WC.appBarSearchIcon"/>
</android.support.v7.widget.Toolbar>
</FrameLayout> <!--****************************Left Pane ****************************-->
There are two panes, right and left, stick together and thus move together. For me, the left pane is the main pane and the right is hidden with a toggle icon to display it. (A view with id appBarSearchIcon).
左右两个窗格粘在一起,因此一起移动。对我来说,左窗格是主窗格,右窗格是隐藏的,带有一个切换图标来显示它。(id 为appBarSearchIcon的视图)。
Remember, there is one viewgroupnamed, SlidingPaneLayoutthat has just two children, The Leftand The Right.
请记住,有一个ViewGroup中命名,SlidingPaneLayout刚刚两个孩子,在左和中右。
And important part in the activity:
活动中的重要部分:
slidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.mainFrame);
// Sets a color for covering left pane(Main Pane)
slidingPaneLayout.setSliderFadeColor(ContextCompat.getColor(context, R.color.searchPaneFadeColor));
// The listener for Opening the Right pane(Hidden pane)
findViewById(R.id.appBarSearchIcon).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view){
slidingPaneLayout.openPane();
}
});
Closing the right pane is done by the API, just like Navigation Drawer.
关闭右侧窗格由 API 完成,就像导航抽屉一样。
回答by DalveerSinghDaiya
The answer is pretty simple: First create a NavigationDrawer Activity.
答案很简单:首先创建一个 NavigationDrawer Activity。
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Then open app_bar_main.layout and assign an id to the parent layout.
然后打开 app_bar_main.layout 并为父布局分配一个 id。
Lets say you gave android:id="@+id/appBarMain"
假设你给了 android:id="@+id/appBarMain"
Just declare the parent viewgroup in MainActivity.class with respective id :
只需在 MainActivity.class 中使用各自的 id 声明父视图组:
and add a drawer listener to the drawerlayout like below:
并将抽屉侦听器添加到抽屉布局,如下所示:
drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
float moveFactor = 0;
moveFactor = (drawerView.getWidth() * slideOffset);
appBarMain.setTranslationX(moveFactor);
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
Add a translation code in OnDrawerSlide() method like above and that's it.
在上面的 OnDrawerSlide() 方法中添加一个翻译代码,就是这样。
回答by Lalit kumar
In second layout set
在第二个布局集中
android:layout_gravity="start"

