android 中DrawerLayout实现抽屉,android项目开发范例大全

DrawerLayout layout = (DrawerLayout) findViewById(R.id.drawer);

ImageView imageView = (ImageView) findViewById(R.id.img);

final ListView listView = (ListView) findViewById(R.id.list);

//获取手机的菜单栏

final ActionBar actionBar = getActionBar();

actionBar.setTitle(“主界面”);

//标题栏的home可以点击

actionBar.setIcon(android.R.drawable.btn_radio);

actionBar.setDisplayHomeAsUpEnabled(true);

actionBar.setHomeButtonEnabled(true);

toggle = new ActionBarDrawerToggle(MainActivity.this,

layout, android.R.drawable.btn_star,

android.R.drawable.btn_star_big_on,

android.R.drawable.btn_star_big_off){

@Override

public void onDrawerClosed(View drawerView) {

super.onDrawerClosed(drawerView);

actionBar.setTitle(“主界面”);

}

@Override

public void onDrawerOpened(View drawerView) {

super.onDrawerOpened(drawerView);

actionBar.setTitle(“菜单界面”);

}

};

//打开或者关闭时的状态

toggle.syncState();

layout.setDrawerListener(toggle);

for (int i = 0; i < string.length; i++) {

list.add(“想发火” + i);

}

if (list != null && list.size() > 0) {

MyAdapter adapter = new MyAdapter(MainActivity.this, list);

listView.setAdapter(adapter);

}

listView.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

Toast.makeText(MainActivity.this, “我想打死你,***”,

Toast.LENGTH_SHORT).show();

}

});

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

return toggle.onOptionsItemSelected(item)||super.onOptionsItemSelected(item);

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

toggle.onConfigurationChanged(newConfig);

}

}

class MyAdapter extends BaseAdapter {

private Context context;

private ArrayList list;

public MyAdapter(Context context, ArrayList list) {

this.context = context;

this.list = list;

}

@Override

public int getCount() {

return list.size();

}

@Override

public Object getItem(int arg0) {

return null;

}

@Override

public long getItemId(int arg0) {

return arg0;

}

@Override

public View getView(int arg0, View arg1, ViewGroup arg2) {

ViewHolder vHolder;

if (arg1 == null) {

arg1 = LayoutInflater.from(context).inflate(R.layout.listview_item,

null);

vHolder = new ViewHolder();

vHolder.textView = (TextView) arg1.findViewById(R.id.list_text);

arg1.setTag(vHolder);

} else {

vHolder = (ViewHolder) arg1.getTag();

}

String string = list.get(arg0);

vHolder.textView.setText(string);

return arg1;

}

class ViewHolder {

TextView textView;

}

}

下面给出相关的布局文件:

<?xml version="1.0" encoding="utf- > **《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》** > > **[开源分享完整内容戳这里](https://github.com/a120464/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)** 8"?>

<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:id="@+id/drawer"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<ImageView

android:id="@+id/img"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:src="@drawable/ic_launcher" />

<LinearLayout

android:layout_width=“300dp”

android:layout_height=“match_parent”

android:layout_gravity=“left”

android:background="#ff0" >

<ListView

android:id="@+id/list"

android:layout_width=“match_parent”

android:layout_height=“match_parent” />

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

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity"

android:background="#ddf">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值