先給大家展示下效果圖:

這個效果是安卓5.0推出 “材料設(shè)計” Ui效果 以前一直沒留意到,寫篇文章當(dāng)成備忘錄
上面的效果圖 用 DrawerLayout和Toolbar實現(xiàn)
布局如下
<?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"><!--標(biāo)題欄--> <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" > <!--內(nèi)容--> <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); //設(shè)置toobar為標(biāo)題欄 setSupportActionBar(toobar); //設(shè)置顯示旋轉(zhuǎn)菜單 getSupportActionBar().setDisplayHomeAsUpEnabled(true); //抽屜布局 drawerLayout = ((DrawerLayout) findViewById(R.id.activity_main)); //activitybar開關(guān) actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name); //同步開關(guān) 如果不寫的話, 滑動開關(guān) 按鈕一直就一個狀態(tài) 不會變化 actionBarDrawerToggle.syncState(); //添加監(jiān)聽 drawerLayout.addDrawerListener(actionBarDrawerToggle); } @Override public boolean onOptionsItemSelected(MenuItem item) { //這里是讓用戶點擊按鈕的時候可以打開抽屜 return actionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); }}新聞熱點
疑難解答
圖片精選