Drawer indicator in lollipop play store

后端 未结 5 2003
-上瘾入骨i
-上瘾入骨i 2020-12-24 08:52

I am using a Nexus 7 with the Android 5.0 preview build.

On this page http://developer.android.com/tools/support-library/index.html

I see

5条回答
  •  庸人自扰
    2020-12-24 09:17

    First, make sure you update to latest SDK. Create new Project in Android Studio, then add appcompat-v7.21.0.+ and appcompat-v4.21.0.+ libraries in your buid.gradle as gradle dependency.

    compile 'com.android.support:appcompat-v7:21.0.2'
    compile 'com.android.support:support-v4:21.0.2'
    

    Add primaryColor and primarycolorDark in your color.xml file.

    
    #2196F3
    #0D47A1
    
    

    Add drawer open/close string value in your strings.xml file.

    
    Lollipop Drawer
    Settings
    open
    close
    
    

    Your activity_my.xml layout file looks like this:

    
    
    
    
    
    
    
        
        
    
            
        
    
        
        
    
            
        
    
    
    
    
    

    Your toolbar.xml layout file looks like this:

    
    
    
    
    

    Your MyActivity.java looks like this: Here your activity must extends ActionBarActivity and set your toolbar as support actionbar.

    import android.content.res.Configuration;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class MyActivity extends ActionBarActivity {
    
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private ListView leftDrawerList;
    private ArrayAdapter navigationDrawerAdapter;
    private String[] leftSliderData = {"Home", "Android", "Sitemap", "About", "Contact Me"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        nitView();
        if (toolbar != null) {
            toolbar.setTitle("Navigation Drawer");
            setSupportActionBar(toolbar);
        }
        initDrawer();
    }
    
    private void nitView() {
        leftDrawerList = (ListView) findViewById(R.id.left_drawer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationDrawerAdapter=new ArrayAdapter( MyActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
        leftDrawerList.setAdapter(navigationDrawerAdapter);
    }
    
    private void initDrawer() {
    
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
    
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
    
            }
    
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
    
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);
    }
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }
    

    Create style.xml file in values-21 folder for android lollipop

    
    
    
    
    
    
    
    
    

    Create your style.xml file in values folder for older versions then android lollipop

    
    
    
    
    
    
    
    

    Your AndroidManifest.xml is looks like this:

    
    
    
    
        
            
                
    
                
            
        
    
    
    
    

    For reference only: you can download complete source code from here : click here

提交回复
热议问题