Android侧拉框Demo

不多说,直接上代码



    
    
        
    
    
    
        
    
    
    



有两点需要注意:1、最外面的布局不能再是5种基本布局了,需要写成 android.support.v4.widget.DrawerLayout

2、侧拉框的布局需要设置layout_gravity属性,左侧拉框就写android:layout_gravity="left",右侧同理。

package com.shenyudi.bmobtest;

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

import java.security.acl.Group;

public class celakuang extends AppCompatActivity {
    private DrawerLayout layout;
    private ListView listView;
    private LinearLayout left_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_celakuang);
        //初始化DrawerLayout
        layout=(DrawerLayout) findViewById(R.id.dl);

        listView= (ListView) findViewById(R.id.list);
        //给listview设置适配器
        listView.setAdapter(new MyAdapter());

    }
    /*若不写此方法,会直接在主界面中显示左右侧拉框里的内容
      不是侧拉才显示*/
    public void left(View v){
        layout.openDrawer(Gravity.LEFT);
    }
    public void right(View view){
        layout.openDrawer(Gravity.RIGHT);
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return View.inflate(getApplication(),R.layout.list_celakuang,null);
        }
    }
}

核心代码:
public void left(View v){
        layout.openDrawer(Gravity.LEFT);
    }
    public void right(View view){
        layout.openDrawer(Gravity.RIGHT);
    }
写这么几行代码就可以实现侧拉框

你可能感兴趣的:(Android侧拉框Demo)