Android从零开始(十七)

阅读更多
知识点:抽屉(SlidingDrawer)

先看一下抽屉打开之前的效果图:

Android从零开始(十七)_第1张图片

抽屉打开之后的效果图:


Android从零开始(十七)_第2张图片


步骤一、抽屉最重要的是布局(核心代码如下)

	
	    
	    
	    
	    
	        
	            
	            
	            
	        
	        
	            
	            
	            
	        
	    
	
    


步骤二、设置打开和关闭图标(核心代码如下)
package com.veryedu.slidingdrawer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.SlidingDrawer;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		SlidingDrawer slidingDrawer=(SlidingDrawer)findViewById(R.id.slidingDrawer);
		slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
			
			@Override
			public void onDrawerOpened() {
				((ImageView)findViewById(R.id.myHandle)).setImageResource(R.drawable.close);
				
			}
		});
		slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
			
			@Override
			public void onDrawerClosed() {
				// TODO Auto-generated method stub
				((ImageView)findViewById(R.id.myHandle)).setImageResource(R.drawable.open);
				
			}
		});
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


源码下载请点击这里:
  • Android从零开始(十七)_第3张图片
  • 大小: 130.3 KB
  • Android从零开始(十七)_第4张图片
  • 大小: 301.6 KB
  • slidingdrawer.zip (1.4 MB)
  • 下载次数: 2
  • 查看图片附件

你可能感兴趣的:(android)