Android5.0 旋轉菜單一實例詳解_Android

來源:互聯網
上載者:User

先給大家展示下效果圖:

這個效果是安卓5.0推出 “材料設計” Ui效果 以前一直沒留意到,寫篇文章當成備忘錄

上面的效果圖 用 DrawerLayout和Toolbar實現

布局如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:background="#fff0ff"  android:orientation="vertical"  tools:context="a.fmy.com.myapplication.MainActivity"><!--標題列-->  <android.support.v7.widget.Toolbar    android:id="@+id/toolbar"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="?attr/colorPrimary"    android:minHeight="?attr/actionBarSize" />  <android.support.v4.widget.DrawerLayout    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#0ff"   >    <!--內容-->    <LinearLayout      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="#ff0"></LinearLayout>    <!--菜單-->    <LinearLayout      android:layout_width="200dp"      android:layout_height="match_parent"      android:layout_gravity="start"      android:background="#f0f" />  </android.support.v4.widget.DrawerLayout></LinearLayout>

activity 代碼

public class MainActivity extends AppCompatActivity {  private Toolbar toobar;  private ActionBarDrawerToggle actionBarDrawerToggle;  private DrawerLayout drawerLayout;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    toobar = (Toolbar) findViewById(R.id.toolbar);    //設定toobar為標題列    setSupportActionBar(toobar);    //設定顯示旋轉菜單    getSupportActionBar().setDisplayHomeAsUpEnabled(true);    //抽屜布局    drawerLayout = ((DrawerLayout) findViewById(R.id.activity_main));    //activitybar開關    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);    //同步開關 如果不寫的話, 滑動開關 按鈕一直就一個狀態 不會變化    actionBarDrawerToggle.syncState();    //添加監聽    drawerLayout.addDrawerListener(actionBarDrawerToggle);  }  @Override  public boolean onOptionsItemSelected(MenuItem item) {    //這裡是讓使用者點擊按鈕的時候可以開啟抽屜    return actionBarDrawerToggle.onOptionsItemSelected(item)        || super.onOptionsItemSelected(item);  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.