DrawerLayout抽屜控件

XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.app1.MainActivity"
    >

    <!-- 主界面的線性佈局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fb00ff"
        android:orientation="vertical">

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打開左邊的抽屜" />


        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打開右邊的抽屜" />

    </LinearLayout>


    <!--  右邊的抽屜:android:layout_gravity="right"-->
    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#55ff00"
        android:orientation="horizontal">

    </LinearLayout>

               <!--左邊的抽屜-->
    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ff1500"
        android:orientation="horizontal"
        >

        <!-- 加載佈局文件  -->
    <include android:id="@+id/drawer_a" layout="@layout/drawer_a"></include>



    </LinearLayout>


</android.support.v4.widget.DrawerLayout>


<pre name="code" class="java">package com.example.administrator.app1;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 抽屜控件
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private DrawerLayout drawer;
    private LinearLayout layout1,layout2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawer= (DrawerLayout) findViewById(R.id.drawer);
        layout1= (LinearLayout) findViewById(R.id.layout1);
        layout2= (LinearLayout) findViewById(R.id.layout2);

        findViewById(R.id.b1).setOnClickListener(this);
        findViewById(R.id.b2).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.b1:
              if(!drawer.isDrawerOpen(layout1)){//如果左邊的抽屜沒有打開
                  drawer.openDrawer(Gravity.LEFT);//打開左邊抽屜

              }
              break;
           case R.id.b2:
              if(!drawer.isDrawerOpen(layout2)){//如果右邊的抽屜沒有打開
                  drawer.openDrawer(Gravity.RIGHT);//打開右邊抽屜
              }
              break;



      }
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章