編輯:關於 Android 程式設計
android.support.v4.widget.DrawerLayout 是實作側邊抽屜的元件,可從左向右或從右向左滑出,主要方法包含 openDrawer()、closeDrawer()、isDrawerOpen()。以下示範主要版面與 Activity 實作。
以 FrameLayout 承載內容,抽屜本身為一個 ListView:
<FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#111111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </FrameLayout>
其中 android:choiceMode 為選中模式(none=無、singleChoice=單選、multipleChoice=多選);android:layout_gravity 的 left/start 表示由左側滑出,right/end 表示由右側滑出。
package com.stone.drawerlayout; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; import java.util.Locale; public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private String[] mPlanetTitles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitle = mDrawerTitle = getTitle(); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mDrawerList.setAdapter(new ArrayAdapter(this, R.layout.drawer_list_item, mPlanetTitles)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mTitle); invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { selectItem(position); } } private void selectItem(int position) { Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); mDrawerLayout.closeDrawer(mDrawerList); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } public static class PlanetFragment extends Fragment { public static final String ARG_PLANET_NUMBER = "planet_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_planet, container, false); int i = getArguments().getInt(ARG_PLANET_NUMBER); String planet = getResources().getStringArray(R.array.planets_array)[i]; int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName()); ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId); getActivity().setTitle(planet); return rootView; } } }
重點提醒:右側抽屜通常用於「動作」而非「導覽」,符合 ActionBar 中導覽在左、動作在右的慣例。使用 ActionBarDrawerToggle 時,務必在 onPostCreate() 與 onConfigurationChanged() 中呼叫,以正確同步切換狀態。
Android 自訂 RatingBar:自由控制星星大小、間距與半星
做商家評分、商品好評度這類功能時,你一定會遇到系統內建的 RatingBar——它最大的問題是星星間距與尺寸幾乎無法調整,只能拿小小的預設圖去硬湊,設計稿永遠對不上。所以
Android AIDL 與遠程 Service:音樂播放器進度同步範例
AIDL 的價值在於讓不同進程之間可以像呼叫本地物件一樣操作遠端 Service。以音樂播放器為例,當播放服務在背景執行時,Activity 仍需要同步進度與控制播放,這
Android 開發學習之路——傳感器初體驗
說到傳感器,種類可謂繁多,加速度、光照、磁傳感器等等不一而足。智能手機之所以「智能」,這些傳感器功不可沒。本文將帶大家動手體驗光照、加速度與磁傳感器這三種常見傳感器。 新
Android 打造竖向新特性引導頁:DirectionalViewPager 實戰
手機管家啟動時有台小飛機在那邊晃啊晃,還挺好玩的,而它展示新功能的方式是竖向的。這兩件事各自都讓我好奇:這種竖向翻頁是怎麼做出來的?那個動來動去的小傢伙又是怎麼實現的?於