可折疊的ToolBar+抽屜菜單NavigationView+浮動按鈕FloatButton

NoaCoburn 7年前發布 | 8K 次閱讀 安卓開發 Android開發 移動開發

使用Material Design風格的ToolBar和抽屜導航

先看個簡單的運行效果

run

主要記錄下布局的寫法

1 用到的Google Design依賴和V7包依賴

compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'

2 主布局結構

主布局結構

3 主布局內容

<android.support.v4.widget.DrawerLayout xmlns:android="

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--CoordinatorLayout 布局屬性:FrameLayout-->
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--AppBar  布局屬性:LinearLayout 默認子元素垂直排列-->
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fitsSystemWindows="true">

            <!--提供了一個可以折疊的Toolbar 布局屬性:FrameLayout-->
            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/colorPrimary"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.5" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?android:attr/actionBarSize"
                    app:contentInsetStart="0dp"
                    app:layout_collapseMode="pin"
                    app:title="@string/app_name" />

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/divider"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.CoordinatorLayout>

    <!--Float Button-->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/float_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="10dp"
        android:src="@drawable/ic_action_important"
        app:elevation="10dp" />

</FrameLayout>

<!--左側抽屜-->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/layout_navigation_header_main"
    app:menu="@menu/main_navigation">

</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout></code></pre>

4 抽屜導航頭布局內容 (layout_navigation_header_main.xml)

<FrameLayout xmlns:android="

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:drawablePadding="5dp"
    android:drawableStart="@mipmap/ic_launcher"
    android:gravity="center"
    android:text="@string/hello"
    android:textColor="@color/icons"
    android:textSize="20sp" />

</FrameLayout></code></pre>

5 界面邏輯很簡單,給RecyclerView綁定數據,抽屜菜單的打開關閉(MainActivity內容)

public class MainActivity extends AppCompatActivity {
    private final String[] data = {"Hello,Word!", 
            "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!",
            "Hello,Word!", "Hello,Android!", "Hello,Material design!"};

private FloatingActionButton floatBtn;
private DrawerLayout drawerLayout;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //設置ToolBar作為ActionBar
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        //設置ActionBar左上角按鈕
        actionBar.setHomeAsUpIndicator(R.drawable.ic_nav);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_main);
    floatBtn = (FloatingActionButton) findViewById(R.id.float_btn);
    floatBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Float Button", Toast.LENGTH_SHORT).show();
        }
    });

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
    MyAdapter myAdapter = new MyAdapter();
    recyclerView.setAdapter(myAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawers();
            } else {
                drawerLayout.openDrawer(GravityCompat.START);
            }
            break;
        case R.id.menu_1:
            Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
            break;
    }
    return super.onOptionsItemSelected(item);
}

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        ViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv.setText(data[holder.getAdapterPosition()]);
    }

    @Override
    public int getItemCount() {
        return data.length;
    }
}

}</code></pre>

6 總結

  1. Google提供的抽屜菜單(NavigationView)確實方便了許多,可以直接通過設置Menu資源作為抽屜菜單的菜單項,可以直接設置layout作為抽屜菜單的頭部
  2. 可折疊的ToolBar
    ( CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + Toolbar )使嵌套滾動的實現變得容易
  3. 浮動按鈕( FloatActionButton )也是方便了實現material design風格

 

來自:http://www.jianshu.com/p/eef5878a7627

 

 本文由用戶 NoaCoburn 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!