【安卓筆記】抽屜式布局----DrawerLayout,安卓布局
效果如下:
DrawerLayout來自support.v4包,所以不用考慮相容性問題。其次,這種布局類似風靡一時的側滑菜單,但是比側滑菜單輕巧許多。下面介紹這種布局的使用方式。
1.在你的項目中匯入support.v4包。2.編輯一個布局,根節點為android.support.v4.widget.DrawerLayout,此節點下只允許有兩個子節點,第一個為將來首頁面的內容,第二個節點即為"抽屜"內容,通常是一個ListView。比如:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈" android:textColor="#000" android:textSize="20sp" /> <Button android:id="@+id/but_open" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="開啟抽屜" /> </LinearLayout> <!-- 一定要加 layout_gravity="start" --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /></android.support.v4.widget.DrawerLayout>
3.編寫代碼,監聽相關事件:
package com.example.drawerlayoutdemo1;import android.app.Activity;import android.os.Bundle;import android.support.v4.widget.DrawerLayout;import android.util.Log;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;public class MainActivity extends Activity{protected static final String TAG = "MainActivity";private DrawerLayout mDrawerLayout = null;private ListView lv = null;private Button but_open = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);lv = (ListView) findViewById(R.id.left_drawer);but_open = (Button) findViewById(R.id.but_open);but_open.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){mDrawerLayout.openDrawer(Gravity.LEFT);}});lv.setAdapter(new ArrayAdapter<String>(this,R.layout.test,R.id.tv_item,new String[]{"條目1","條目2","條目3"}));mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){@Overridepublic void onDrawerClosed(View drawerView){Log.i(TAG,"抽屜關閉了...");}@Overridepublic void onDrawerOpened(View drawerView){Log.i(TAG,"抽屜開啟了...");}@Overridepublic void onDrawerSlide(View drawerView, float slideOffset){Log.i(TAG,"抽屜在滑動...");}@Overridepublic void onDrawerStateChanged(int newState){switch (newState){case DrawerLayout.STATE_DRAGGING:Log.i(TAG,"拖動狀態");break;case DrawerLayout.STATE_IDLE:Log.i(TAG,"靜止狀態");break;case DrawerLayout.STATE_SETTLING:Log.i(TAG,"設定狀態");break;default:break;}}});lv.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id){switch (position){case 0:Log.i(TAG,"條目1被點擊");break;case 1:Log.i(TAG,"條目2被點擊");break;case 2:Log.i(TAG,"條目3被點擊");break;}}});}}