Android Design Support控制項之DrawerLayout簡單使用

來源:互聯網
上載者:User

標籤:start   sig   代碼   ack   mod   parent   ++   wpa   comment   

DrawerLayout能夠讓我們在項目中非常方便地實現側滑菜單效果。如今主流的應用如QQ等都
採用的這樣的效果。

這兩天也是在學習Android Design Support的相關知識。網上有關這方面的文章介紹非常多。可是為了方便以後使用,還是把學習的知識做個簡單記錄。這次的代碼也是在上一篇部落格Android Design Support控制項介紹之TabLayout的基礎上加入的布局和代碼。
主介面布局:

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/drawer_view"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.design.widget.NavigationView        android:id="@+id/navigation_view"        android:layout_width="200dp"        android:layout_height="match_parent"        android:layout_gravity="left"        android:fitsSystemWindows="true"        app:headerLayout="@layout/content_main"><!--content_main與上篇文章中一樣-->    </android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

側滑菜單左側的布局

<?xml version="1.0" encoding="utf-8"?

><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="菜單ONE" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="菜單TWO" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="菜單THREE" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="菜單FOUR" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="菜單FIVE" /></LinearLayout>

Fragment介面布局

<?

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="菜單ONE" /></LinearLayout>

主介面代碼,TestAdapter 及TestFragment代碼也同上一篇文章一樣。

public class MainActivity extends FragmentActivity {    //便捷實現標籤顯示    private TabLayout tab_layout;    private ViewPager viewpager;    private TestAdapter mAdapter;    private List<Fragment> fragments;    private List<String> titles;    private DrawerLayout drawer_view;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.drawer_layout);        initViews();        initViewPages();    }    private void initViewPages() {        fragments = new ArrayList<>();        titles = new ArrayList<>();        for (int i = 0; i < 4; i++) {            String title="TAB" + i;            titles.add(title);            tab_layout.addTab(tab_layout.newTab().setText(title));//加入Tab標題            Fragment fragment = new TestFragment();            Bundle bundle = new Bundle();            bundle.putString("content", "這是第" + i + "個Fragment頁面");            fragment.setArguments(bundle);            fragments.add(fragment);        }        mAdapter = new TestAdapter(this.getSupportFragmentManager(), titles, fragments);        viewpager.setAdapter(mAdapter);        tab_layout.setupWithViewPager(viewpager);        tab_layout.setTabsFromPagerAdapter(mAdapter);    }    private void initViews() {        tab_layout = (TabLayout) findViewById(R.id.tab_layout);        viewpager = (ViewPager) findViewById(R.id.viewpager);        drawer_view=(DrawerLayout)findViewById(R.id.drawer_view);        drawer_view.closeDrawer(GravityCompat.START);    }}


Android Design Support控制項之DrawerLayout簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.