先给大家展示下效果图:
这个效果是安卓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); }}
新闻热点
疑难解答