DrawerLayout

//布局
 	<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00f"
        >
      <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher" />

    </RelativeLayout>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/dl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--第一个子布局是主页面-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="缘分是一种很美妙的东西"
                android:textSize="30dp"
                />
        </LinearLayout>
        <!--第二个子布局是侧滑界面-->

        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="@color/colorPrimary">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></ListView>

        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>
//activit
public class OtherActivity extends AppCompatActivity {

    private DrawerLayout dl;
    private TextView text;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otheractivity);
      //控件
        dl = findViewById(R.id.dl);
        text = findViewById(R.id.text);
        img = findViewById(R.id.img);
        //监听
        dl.setDrawerListener(new DrawerLayout.DrawerListener() {

//             当抽屉被滑动的时候调用此方法
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {
              //  Toast.makeText(MainActivity.this, "onDrawerSlide", Toast.LENGTH_SHORT).show();

            }
//              当一个抽屉被完全打开的时候被调用
            @Override
            public void onDrawerOpened(@NonNull View view) {
                Toast.makeText(OtherActivity.this, "打开", Toast.LENGTH_SHORT).show();
            }
            //当一个抽屉完全关闭的时候调用此方法
            @Override
            public void onDrawerClosed(@NonNull View view) {
                Toast.makeText(OtherActivity.this, "关闭", Toast.LENGTH_SHORT).show();
            }
//              当抽屉滑动状态改变的时候被调用
            @Override
            public void onDrawerStateChanged(int i) {
                Toast.makeText(OtherActivity.this, "StateChanged", Toast.LENGTH_SHORT).show();

            }
        });


      /*  text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dl.openDrawer(Gravity.RIGHT);
            }
        });*/

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if (dl.isDrawerOpen(Gravity.LEFT)){
                     //关闭抽屉
                     dl.closeDrawer(Gravity.LEFT);
                     Toast.makeText(OtherActivity.this, "关闭抽屉", Toast.LENGTH_SHORT).show();
                 }else{
                     //打开抽屉
                     dl.openDrawer(Gravity.LEFT);
                     Toast.makeText(OtherActivity.this, "打开抽屉", Toast.LENGTH_SHORT).show();

                 }
            }
        });
    }
}


相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页