[Android] SlidingMenu使用筆記
說明:
Sliding Menu在手機的應用程式中是相當常見的設計,除了以前提到的
Navigation Drawer作法之外,還可以使用本文所提的Sliding Menu(jfeinstein10)的第三方資源來達成。
該資源提供豐富設定供開發者來使用,甚至有左右兩方的選單可以設定。
成果:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chengyu.actionbartest; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; | |
public class ActionBarActivity extends AppCompatActivity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// slidingMenu settings | |
slidingMenuSet(); | |
setContentView(R.layout.activity_action_bar); | |
} | |
private void slidingMenuSet() { | |
setTitle("SlidingMenu"); | |
SlidingMenu menu = new SlidingMenu(this); | |
menu.setMenu(R.layout.left_menu); | |
menu.setMode(SlidingMenu.LEFT); | |
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); | |
//menu.setShadowWidthRes(R.dimen.shadow_width); | |
//menu.setShadowDrawable(R.drawable.shadow); | |
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); | |
menu.setFadeDegree(0.35f); | |
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
getSupportActionBar().setHomeButtonEnabled(true); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_action_bar, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Default screen margins, per the Android Design guidelines. --> | |
<dimen name="activity_horizontal_margin">16dp</dimen> | |
<dimen name="activity_vertical_margin">16dp</dimen> | |
<dimen name="shadow_width">15dp</dimen> | |
<dimen name="slidingmenu_offset">60dp</dimen> | |
</resources> |
備註:
在project中需要導入第三方library。
有需要與時間的話,再來追加影片與說明。
留言
張貼留言