今天准备做一个侧滑,以前本来是用的SlidingMenu实现,忽然想起来,前几个月有人跟我说DrawerLayout也可以实现侧滑,那么就来试一下
布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="@color/txt_gray"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/colorAccent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="shflksjdflkjsdfs"
android:textColor="@color/txt_orange"
android:textSize="20dp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>注意:里边有一行关键的代码:(红色部分的代码)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
<span style="color:#ff0000;">android:layout_gravity="end"</span>
android:background="@color/colorAccent"
android:gravity="center">该代码表示,是从左右那个位置滑动(上下没有试,可以自己尝试下),必须写,不写不能实现滑动功能
activity代码:
public class SevenActivity extends Activity {
private ActionBarDrawerToggle mActionBarDrawerToggle;
private DrawerLayout myDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
myDrawerLayout = (DrawerLayout) findViewById(R.id.myDrawerLayout);
//监听
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, R.mipmap.ic_launcher,
<span style="white-space:pre"> </span>R.string.drawer_open, R.string.drawer_close);
}至此,一个侧滑就完成了,我做的是左滑的功能。
<pre name="code" class="java" style="line-height: 26px;"> mActionBarDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, R.mipmap.ic_launcher,
<span> </span>R.string.drawer_open, R.string.drawer_close);
这是drawerlayout的监听,他有三种方法
1. 设置DrawerLayout.DrawerListener作为监听器类,里面包含四个回调函数。
代码如下:
2. 设置DrawerListener的子类SimpleDrawerListener,使用这个类的时候不必实现全部的回调函数,可以根据自己的需要重写相应的方法。
代码如下:
3. 使用DrawerListener的子类ActionBarDrawerToggle。一般与ActionBar结合使用。
代码如下:
所谓监听,无非就是打开抽屉之后的操作,关闭抽屉之后的操作嘛,
调用closeDrawer()和openDrawer()可以关闭和打开抽屉。
调用closeDrawer()和openDrawer()可以关闭和打开抽屉。
1385

被折叠的 条评论
为什么被折叠?



