Android Material Design

来源:互联网 发布:mac美图秀秀 编辑:程序博客网 时间:2024/09/21 09:05

最近有的做手机项目,于是又接触 Android 了。

然后对 Material 比较感兴趣,总结些经验。


这篇文章主要是介绍 drawerlayout 。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v7.widget.Toolbar        android:id="@+id/toolbar"        android:layout_width="match_parent"        android:layout_height="?android:attr/actionBarSize"        />    <android.support.v4.widget.DrawerLayout        android:id="@+id/drawer_layout"        android:layout_width="match_parent"        android:layout_height="match_parent">        <!-- The main content view -->        <FrameLayout            android:background="#ff0000"            android:layout_height="match_parent"            android:layout_width="match_parent">        </FrameLayout>        <!-- The navigation drawer -->        <RelativeLayout            android:background="#0000ff"            android:layout_height="match_parent"            android:layout_width="match_parent"            android:layout_gravity="start"            android:id="@+id/navigation_drawer">        </RelativeLayout>    </android.support.v4.widget.DrawerLayout></LinearLayout>


Drawerlayout 下一般放两到三个布局,也可以直接放 fragment,动态替换 fragment 也可以。

三个布局分别代表,主页面,左边栏,右边栏,区分的关键就是 layout_gravity="start|left"。

照这种布局,侧边栏是不会把 toolbar 遮住的。

https://github.com/blackcj/android-toolbar-with-drawer 这里补充了遮护 toolbar 的例子。

如果不想边栏遮住状态栏,可以添加   android:fitsSystemWindows="true"。


如果想要这种很酷的效果:

http://chrisrenke.com/drawerarrowdrawable/

public class ToolbarActivity extends ActionBarActivity {    private ActionBarDrawerToggle toggle;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_toolbar);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);        toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);//        toggle.setDrawerIndicatorEnabled(true);        drawerLayout.setDrawerListener(toggle);    }    @Override    protected void onPostCreate(Bundle savedInstanceState) {        super.onPostCreate(savedInstanceState);        toggle.syncState();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.toolbar, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        if (toggle.onOptionsItemSelected(item))            return true;        int id = item.getItemId();        return id == R.id.action_settings || super.onOptionsItemSelected(item);    }


有些代码好像没什么用处,可以试着删掉。

这里没有用 fragment,所以 activity recreate 时也不用去管。

顺便一提,22 里 ActionBarActivity 也过时了。












0 0
原创粉丝点击