Android Material Design > Toolbar

Android Material Design > Toolbar_第1张图片

 水很深 很深   

 看注释  theme  / dark  light


  
    
    

    

//https://me.csdn.net/qq_30447263
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.mytoolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//隐藏  显示 箭头


//        getSupportActionBar().setTitle("SSSSS");//设置title  这里建议直接在XML 里边 设置title  可以直接使用contentInsetStart 居中
//        getSupportActionBar().setDisplayShowTitleEnabled(true);//隐藏  显示title


//        toolbar.setNavigationIcon(R.mipmap.icon_see);//设置对应的图片
//        app:navigationIcon="@drawable/icon_a"    xml


//        Fragment中 setHasOptionsMenu(true);  ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);  onCreateOptionsMenu
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tool_bar, menu);//加载menu布局
        return true;
    }

    /**
     * 由于google 不在支持menu 中显示图标 所以 就这样
     * https://blog.csdn.net/shangming150/article/details/77914110
     *
     * @param featureId
     * @param menu
     * @return
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    //设置点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tab_one:
                Toast.makeText(this, "tab_one", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tab_two:
                Toast.makeText(this, "tab_two", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tab_three:
                Toast.makeText(this, "tab_three", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tab_four:
                Toast.makeText(this, "tab_four", Toast.LENGTH_SHORT).show();
                break;

            case android.R.id.home:
                Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}

styles



    
    


    
    

    
    

    
    

 

你可能感兴趣的:(Ma什么Design...)